代码永远不会执行错误



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)

更新

如果 else 语句,请按如下方式修改语句:

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

您没有检查完成处理程序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也一样.希望对您有所帮助。

最新更新