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";
}
}
];
}
}
答案 0 :(得分:1)
您的代码中的问题是您正在检查枚举,因为枚举的值不为零,因此它将始终为true并且不会进入else部分,因此您需要更改两个if语句中的条件以检查错误像这样的代码(我不确定语法,但希望你能得到这个想法)
if (error.code == FIRAuthErrorCodeUserNotFound)
if (error.code == FIRAuthErrorCodeInvalidEmail)
<强>更新强>
修改if if else语句如下:
if (error) {
NSLog(@"%@", error)
}else {
//code when there is no error
}
答案 1 :(得分:1)
您无法检查完成处理程序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
。希望它可以帮到你。