【问题标题】:Code will never be executed error代码永远不会被执行错误
【发布时间】:2017-08-26 08:02:22
【问题描述】:

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";
                                 }
                             }
     ];
}
}

【问题讨论】:

  • 这个警告在哪一行?
  • 我将编辑代码以显示警告出现的位置。
  • @AdityaSrivastava 您现在应该能够看到更新的代码,其中显示了两个错误的位置。注意:else 语句中的所有内容在技术上都适用于这个“代码永远不会被执行”错误,所以编辑里面的内容不会产生任何结果,我相信外部语法有问题。
  • 发表了我的答案。检查一下

标签: ios objective-c firebase firebase-authentication


【解决方案1】:

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

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

更新

修改if else语句如下:

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

【讨论】:

  • 这非常有用,简短而有益。谢谢。
  • 我遇到了 if 和 else 语句都被执行的问题。我的意思是文本框将值更改为存储在 if 语句中的值,但转换仍然发生到下一个视图控制器...
  • @TrojanTheHorse 这是不同的问题,但无论如何请显示您正在使用的更新代码
  • @TrojanTheHorse 在每个 if 语句之前添加 print(error.code) 并告诉我输出,请从您发布的答案中删除代码,我已经编辑了问题
  • 我正在使用 NSLog(@"%ld", (long)error.code);相反,因为打印功能给了我“隐式声明......”错误。
【解决方案2】:

您无法检查完成处理程序NSError,这就是编译器发出此警告的原因。所以,在你的signInWithEmail中检查它

                          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];
                             }

对于您的 createUserWithEmail 也是如此。希望对你有帮助。

【讨论】:

  • 连同其他答案,这非常有用,谢谢。
  • 虽然,现在的行为是,即使它显示错误语句,但发生的情况是它仍然执行 else...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 2020-12-07
  • 2021-08-19
  • 2016-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多