如何使用FirebaseAuth错误代码?

时间:2017-07-27 01:28:13

标签: ios swift firebase firebase-authentication

我正在尝试使用Firebase中的AuthErrorCode,但我一直在收到错误。这是我的代码:

private func handleErrors(err: NSError, loginHandler: LoginHandler?) {

    if let errCode = AuthErrorCode(rawValue: err.code) {

        switch errCode {

        case .errorCodeWrongPassword:  //Enum case 'errorCodeWrongPassword' not found in type 'AuthErrorCode'
            loginHandler?(LoginErrorCode.WRONG_PASSWORD)
            break

        case .errorCodeInvalidEmail:  //Enum case 'errorCodeInvalidEmail' not found in type 'AuthErrorCode'
            loginHandler?(LoginErrorCode.INVALID_EMAIL)
            break

        case .errorCodeUserNotFound:  //Enum case 'errorCodeUserNotFound' not found in type 'AuthErrorCode'
            loginHandler?(LoginErrorCode.USER_NOT_FOUND)
            break

        case .errorCodeEmailAlreadyInUse:  //Enum case 'errorCodeEmailAlreadyInUse' not found in type 'AuthErrorCode'
            loginHandler?(LoginErrorCode.EMAIL_ALREADY_IN_USE)
            break

        case .errorCodeWeakPassword:  //Enum case 'errorCodeWeakPassword' not found in type 'AuthErrorCode'
            loginHandler?(LoginErrorCode.WEAK_PASSWORD)
            break

        default:
            loginHandler?(LoginErrorCode.PROBLEM_CONNECTING)
            break

        }

    }

}

任何人都知道我做错了什么?如果您在我的代码中没有看到错误,那么它们就是:

Enum case 'errorCodeWrongPassword' not found in type 'AuthErrorCode'

Enum case 'errorCodeInvalidEmail' not found in type 'AuthErrorCode'

Enum case 'errorCodeUserNotFound' not found in type 'AuthErrorCode'

Enum case 'errorCodeEmailAlreadyInUse' not found in type 'AuthErrorCode'

Enum case 'errorCodeWeakPassword' not found in type 'AuthErrorCode'

如果你们有任何想法,请告诉我们!

1 个答案:

答案 0 :(得分:3)

它们在AuthErrorCode.weakPassword中以Swift导入,例如:从所有案例值中删除errorCode

private func handleErrors(err: NSError, loginHandler: LoginHandler?) {

    guard let errCode = AuthErrorCode(rawValue: err.code)
        else { return }

        switch errCode {

        case .wrongPassword:
            loginHandler?(LoginErrorCode.WRONG_PASSWORD) // Transform to wrongPassword
        case .invalidEmail:
            loginHandler?(LoginErrorCode.INVALID_EMAIL)
        case .userNotFound:
            loginHandler?(LoginErrorCode.USER_NOT_FOUND)
        case .emailAlreadyInUse:
            loginHandler?(LoginErrorCode.EMAIL_ALREADY_IN_USE)
        case .weakPassword:
            loginHandler?(LoginErrorCode.WEAK_PASSWORD)
        default:
            loginHandler?(LoginErrorCode.PROBLEM_CONNECTING)
        }

}



迅捷的建议:

  • 不要在switch语句中使用break,在案例完成后默认会中断fallthrough允许您在匹配后检查每个案例)
  • 切换NSError
  • Error
  • LoginErrorCode是您的自定义类吗?考虑切换到案例的Swift> = 3语法(较低的驼峰案例)