我一直在跟随一段时间的迅速课程,并跟随在线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就可以编译代码,但只要出现错误就会停止编译。
预先感谢您的时间!
我不熟悉firebase,但是你确定编译错误的错误与第二个函数中的'error'是同一类型(又名NSError)吗?我有一种感觉,firebase会将你自己的错误类型返回给你,而不是NSError ...(因为在参数列表中省略了类型,所以我不知道) – Fonix
Firebase确实有它自己的错误代码列表,我也认为我们正在获取Firebase错误类型,这些错误类型都应该是Int的。 你能向我解释为什么这两个func的错误是不同类型的? – Takis