swift 中的 firebase 身份验证和 Firestone 错误代码

firebase auth and Firestone error code in swift

我遵循了一些教程,但没有人接缝工作我需要获取 AuthErrorCode 用于 firebase 身份验证和 Firestone 用于使本地化这是我的代码

这是我需要调用 errorHandlingFunction 的登录函数

Auth.auth().signIn(withEmail: emailTextField, password: passwordTextField) {result, error in
                if error != nil {
                    self.alertMessage = self.errorHandling(error: error! as NSError)
                    self.showAlert.toggle()
                    self.isLoading = false
                    return
                }

func errorHandling(error: NSError) -> String {
    
        if let err = error as NSError?, let code = AuthErrorCode(rawValue: error.code)
        {
    
            switch code {
            case .accountExistsWithDifferentCredential:
                return String(localized: "Account already exist with different credetial", table: "Localization", comment: "alert message")
            case .credentialAlreadyInUse:
                return String(localized: "Credential are already in use", table: "Localization", comment: "alert message")
            case .unverifiedEmail:
                return String(localized: "An email link was sent to your account, please verify it before loggin in", table: "Localization", comment: "alert message")
            case .userDisabled:
                return String(localized: "User is currently disabled", table: "Localization", comment: "alert message")
            case .userNotFound:
                return String(localized: "Canno't find the user, try with different credential", table: "Localization", comment: "alert message")
            case .weakPassword:
                return String(localized: "Password is too weak", table: "Localization", comment: "alert message")
            case .networkError:
                return String(localized: "Error in network connection", table: "Localization", comment: "alert message")
            case .wrongPassword:
                return String(localized: "Password is wrong", table: "Localization", comment: "alert message")
            case .invalidEmail:
                return String(localized: "Email is not valid", table: "Localization", comment: "alert message")
            default:
                return String(localized: "Unknown error occurred", table: "Localization", comment: "alert message")
            }
        }
    }

但是我从编译器那里得到这个错误

Cannot convert value of type 'Int' to expected argument type 'AuthErrorCode.Code'

有解决办法吗?还有凡士通?

谢谢

要初始化代码,您应该使用

let nsError = error as NSError
AuthErrorCode.Code.init(rawValue: nsError.code)

根据错误,问题可能是由于函数需要 Code 而不是 Int。让我们简化以处理所有情况:

Auth.auth().signIn(withEmail: user, password: pw, completion: { (auth, error) in
    if let maybeError = error { //if there was an error, handle it
        let err = maybeError as NSError
        switch err.code {
        case AuthErrorCode.wrongPassword.rawValue:
            print("wrong password")
        case AuthErrorCode.invalidEmail.rawValue:
            print("invalid email")
        case AuthErrorCode.accountExistsWithDifferentCredential.rawValue:
            print("accountExistsWithDifferentCredential")
        ... add the rest of the case statements
        default:
            print("unknown error: \(err.localizedDescription)")
        }
    } else { //there was no error so the user could be auth'd or maybe not!
        if let _ = auth?.user {
            print("user is authd")
        } else {
            print("no authd user")
        }
    }
})