ios - 代码永远不会执行错误

标签 ios objective-c firebase firebase-authentication

Xcode 显示代码永远不会执行的警告,但我不明白为什么。谁能启发我? (错误发生在完成处理程序内部的内部 if 和 else 语句中,用于两个验证语句)

- (IBAction)siginButtonPressed:(UIButton *)sender
{
    if (isLogin)
{
    [[FIRAuth auth] signInWithEmail:_usernameTextField.text
                           password:_passwordTextField.text
                         completion:^(FIRUser *user, NSError *error)
         {
             if (FIRAuthErrorCodeUserNotFound)
             {
                 _credentialVerification.text = @"Invalid Credentials";
                 _entryValidation.text = @"An account with the listed credentials was not found, please proceed to registration screen via the navigation bar at the top of the applications user interface";
             }
             else
             {
 /*(Warning 1)*/ _credentialVerification.text = @"Valid Credentials";
                 [self performSegueWithIdentifier:@"toMainScreen" sender:self];
             }
         }

     ];
}
else
{
    [[FIRAuth auth] createUserWithEmail:_usernameTextField.text
                               password:_passwordTextField.text
                             completion:^(FIRUser *_Nullable user, NSError *_Nullable error)
         {
             if (FIRAuthErrorCodeInvalidEmail)
             {
                 _credentialVerification.text = @"Invalid Email";
             }
             else
             {

 /*(Warning 2)*/ _entryValidation.text = @"Account creation was succesful, please proceed to the login screen via the navigation bar at the top of the applications user interface";
             }
         }

     ];
}

编辑:

- (IBAction)siginButtonPressed:(UIButton *)sender
{
if (isLogin)
{
    [[FIRAuth auth] signInWithEmail:_usernameTextField.text
                           password:_passwordTextField.text
                         completion:^(FIRUser *user, NSError *error)
                         {
                             if (error.code == FIRAuthErrorCodeUserNotFound)
                             {
                                 _credentialVerification.text = @"Invalid Credentials";
                                 _entryValidation.text = @"An account with the listed credentials was not found, please proceed to registration screen via the navigation bar at the top of the applications user interface";
                             }
                             else
                             {
                                 _credentialVerification.text = @"Valid Credentials";
                                 [self performSegueWithIdentifier:@"toMainScreen" sender:self];
                             }
                         }
     ];
}
else
{
    [[FIRAuth auth] createUserWithEmail:_usernameTextField.text
                               password:_passwordTextField.text
                             completion:^(FIRUser *_Nullable user, NSError *_Nullable error)
                             {
                                 if (error.code == FIRAuthErrorCodeInvalidEmail)
                                 {
                                     _credentialVerification.text = @"Invalid Email";
                                 }
                                 else
                                 {
                                     _entryValidation.text = @"Account creation was succesful, please proceed to the login screen via the navigation bar at the top of the applications user interface";
                                 }
                             }
     ];
}
}

最佳答案

您的代码中的问题是您正在检查枚举,因为枚举的值不为零,因此它将始终为真并且不会进入 else 部分,因此您需要更改两个 if 语句中的条件以检查错误像这样的代码(我不确定语法但希望你能明白)

if (error.code == FIRAuthErrorCodeUserNotFound)
if (error.code == FIRAuthErrorCodeInvalidEmail)

更新

修改你的 if else 语句如下:

if (error) {
       NSLog(@"%@", error)
}else {
       //code when there is no error    
}

关于ios - 代码永远不会执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45893299/

相关文章:

swift - 如何使用 firebase 快速向新创建的用户添加信息

ios - UITableViewCell : get the exact width of UILabel in cell

公开只读但具有私有(private) setter 的 Objective-C 属性

android - 带有 ProGuard 的 Firebase

ios - 许多文件的 attributesOfItemAtPath

objective-c - Objective-C:完成间隔计时器

javascript - 访问另一个对象内部的对象属性

ios - 通过缩放将 UIView 放置在 UIImageView 上

ios - 比较包含 NSStrings 的 NSDictionaries

iOS 11 - 如何读取/解析来自 CoreNFC 的 NDEF 消息?