简体   繁体   English

Swift中的错误投射问题

[英]Problems With Error Casting In Swift

Not sure if this a bug or an intended feature. 不确定这是错误还是预期功能。

To create a user with an email and password in Firebase, I've been using the following code: 要在Firebase中使用电子邮件和密码创建用户,我一直在使用以下代码:

FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
    if let error = error {
        guard let error = error as? FIRAuthErrorCode else { return } // ALWAYS FAILS 
        ...code...
    }
    ...code...
}

The error parameter in the completion handler for the method cannot be cast as FIRAuthErrorCode ; 该方法的完成处理程序中的error参数不能转换为FIRAuthErrorCode ; it always fails. 它总是失败。 Is that a bug, or is that the expected behaviour? 这是一个错误,还是预期的行为?

Edit: I am aware that error codes can be used to distinguish between the different types of FIRAuthErrorCode errors. 编辑:我知道可以使用错误代码来区分不同类型的FIRAuthErrorCode错误。 It's just not readable, and it doesn't make much sense for the error parameter in the completion handler to be not of be of type FIRAuthErrorCode . 它只是不可读,并且完成处理程序中的error参数不是FIRAuthErrorCode类型也没有多大意义。 The cases and error codes of FIRAuthErrorCode can be found here . 可以在这里找到FIRAuthErrorCode的情况和错误代码。

Have you tried using guard let error = error as! FIRAuthErrorCode else { return } 您是否尝试过使用guard let error = error as! FIRAuthErrorCode else { return } guard let error = error as! FIRAuthErrorCode else { return } to force the casting and check whether the return is nil or not? guard let error = error as! FIRAuthErrorCode else { return }强制转换并检查return是否为nil?

您应检查firebase中signIn方法的文档,以检查此方法可以发送的所有可能的错误类型,然后在代码中的保护块中检查这些类型的错误。

Try this, it's how I do my login and it seems to work great. 试试这个,这是我登录的方式,它看起来很棒。

FIRAuth.auth()?.signIn(withEmail: EmailField.text!, password: PasswordField.text!, completion: { user, error in
        if error == nil {
        print("Successfully Logged IN \(user!)")
        self.performSegue(withIdentifier: "Login", sender: self)
        }
    })

I just check that there's not an error with the signin process, then perform my segue. 我只是检查登录过程是否没有错误,然后执行我的segue。

After contacting Firebase support about the issue, they've said that the errors that are passed back in completion handlers are just Error objects. 与Firebase支持人员联系后,他们说在完成处理程序中传回的错误只是Error对象。 They weren't FIRAuthErrorCode objects. 它们不是FIRAuthErrorCode对象。 To test for the various FIRAuthErrorCode cases, one would have to do something like this: 要测试各种FIRAuthErrorCode情况,必须执行以下操作:

FIRAuth.auth()?.signIn(withEmail: email, password: password) { (user, error) in
    if let error = error {
        guard let error = FIRAuthErrorCode(rawValue: error._code) else {
            fatalError("This should never be executed")
        }
        switch error {
        case .errorCodeInvalidEmail: ...
        case .errorCodeWrongPassword: ...
        default: ...

        }
        ...code...
    }
    ...code...
}

^ This preserves readability and makes error handling more intuitive. ^这样可以保持可读性并使错误处理更加直观。 And, there is no need for error casting! 并且,无需进行错误投射!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM