2016-08-17 88 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错误!._代码解决了我的问题。谢谢你们的回应,并试图解决这个问题,非常感谢。

相关问题