2016-08-17 75 views
2

我一直在跟隨一段時間的迅速課程,並跟隨在線tuts。我能找到的大部分問題,但我可以用這個幫助。價值的類型沒有成員

在應用程序中,我們正在處理可能發生通過火力地堡FIRAuth登錄用戶的錯誤,我的代碼看起來是這樣的:

class AuthService { 

private static let _instance = AuthService() 

static var instance: AuthService { 
    return _instance 
} 

func login(email: String, password: String, onComplete: Completion?) { 
    FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: {(user, error) in 

     if error != nil { 

      if let errorCode = FIRAuthErrorCode(rawValue: error!.code) { 
       if errorCode == .errorCodeUserNotFound { 
        FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: {(user, error) in 
         if error != nil { 
          self.handleFirebaseError(error: error!, onComplete: onComplete) 
         } else { 
          if user?.uid != nil { 
           //Sign in 
           FIRAuth.auth()?.signIn(withEmail: email, password: password, completion: {(user, error) 
           in 
            if error != nil { 
             self.handleFirebaseError(error: error!, onComplete: onComplete) 
            } else { 
             onComplete?(errMsg: nil, data: user) 
            } 
           }) 
          } 
         } 
        }) 
       } 
      } else { 
       self.handleFirebaseError(error: error!, onComplete: onComplete) 
      } 
     } else { 
      onComplete?(errMsg: nil, data: user) 
     } 

    }) 
} 

func handleFirebaseError(error: NSError, onComplete: Completion?) { 
    print(error.debugDescription) 
    if let errorCode = FIRAuthErrorCode(rawValue: error.code) { 

     switch(errorCode) { 
     case .errorCodeInvalidEmail: 
      onComplete?(errMsg: "Invalid email adress", data: nil) 
      break 
     case .errorCodeWrongPassword: 
      onComplete?(errMsg: "Invalid password", data: nil) 
      break 
     case .errorCodeEmailAlreadyInUse, .errorCodeAccountExistsWithDifferentCredential: 
      onComplete?(errMsg: "Email already in use", data: nil) 
      break 

     default: 
      onComplete?(errMsg: "There was a problem authenticating, try again", data: nil) 
      break 
     } 

} 

在編譯我在在第一時間拿到FUNC錯誤:

if let errorCode = FIRAuthErrorCode(rawValue: error!.code) 

說「類型'錯誤'的值沒有成員的'代碼'」。第二個func使用完全相同的代碼行,但沒有錯誤。我嘗試了各種各樣的東西,如解開或不成功。

例如,添加一個0就可以編譯代碼,但只要出現錯誤就會停止編譯。

預先感謝您的時間!

+1

我不熟悉firebase,但是你確定編譯錯誤的錯誤與第二個函數中的'error'是同一類型(又名NSError)嗎?我有一種感覺,firebase會將你自己的錯誤類型返回給你,而不是NSError ...(因爲在參數列表中省略了類型,所以我不知道) – Fonix

+0

Firebase確實有它自己的錯誤代碼列表,我也認爲我們正在獲取Firebase錯誤類型,這些錯誤類型都應該是Int的。 你能向我解釋爲什麼這兩個func的錯誤是不同類型的? – Takis

回答

0

如果我正確地讀這篇文章,你問的是什麼樣的這兩行代碼(你形容爲之間的區別「如出一轍」

if let errorCode = FIRAuthErrorCode(rawValue: error!.code) { 
if let errorCode = FIRAuthErrorCode(rawValue: error.code) { 

當印刷在一起在第一行error!意味着在那一點上錯誤肯定不會是零,所以你會得到一個運行時錯誤,如果是的話,在第二行error可以是零,如果是,那麼let語句將會下降直至else或接下來的聲明(如適用)。

作爲一般規則,避免使用!在你的代碼中強制非零值,除非你101%確定它永遠是這種情況。

+0

謝謝你的評論。有一個問題,因爲代碼檢查上面的行中沒有錯誤,我想它可以安全地解包。 我不明白的是爲什麼我得到no成員通知。 – Takis

+0

可能是因爲第一行自相矛盾。你正在檢查一個不可能爲零的可能的零值。錯誤!.code永遠不會返回一個零值,因此檢查零是沒有意義的。 –

0

好吧,所以更改錯誤!.code錯誤!._代碼解決了我的問題。謝謝你們的迴應,並試圖解決這個問題,非常感謝。

相關問題