Iphone 程序因API调用而崩溃
我有两页: 登录页面和注册页面: 对于登录页面,我使用以下方法获取信息并将失败或成功响应传递给用户:Iphone 程序因API调用而崩溃,iphone,json,api,Iphone,Json,Api,我有两页: 登录页面和注册页面: 对于登录页面,我使用以下方法获取信息并将失败或成功响应传递给用户: - (void)login:(UIButton *)sender { [self.appDel.api loginWithEmail:self.email.text password:self.password.text completionHandler:^(id response, NSErro
- (void)login:(UIButton *)sender
{
[self.appDel.api loginWithEmail:self.email.text
password:self.password.text
completionHandler:^(id response, NSError *error) {
if (error) {
NSLog(@"ERROR: %@", error);
} else {
if ([[response objectForKey:Status] isEqualToString:Success]) {
self.currentUser = [response objectForKey:kResponseUser];
self.currentUserProfile = [response objectForKey:Profile];
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
} else {
NSLog(@"FAIL: %@", response);
[[[UIAlertView alloc] initWithTitle:@"Login Failed"
message:[response description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
}];
}
使用此登录页面时,如果用户未填写两个必填字段,或者其中一个字段已填充,另一个字段为空,然后按login键,他将收到失败消息
然而,在注册案例中,当文本字段为空并且我们单击注册时,程序崩溃。我应该如何改变这一点:
-(void)signUp:(UIButton *)sender
{
[self.api signup:self.Email.text
password:self.Password.text
confirmationPassword:self.ConfirmPassword.text
firstName:self.FirstName.text
completionHandler:^(id response, NSError *error) {
if (error) {
NSLog(@"ERROR: %@", error);
} else {
if ([[response objectForKey:Status] isEqualToString:Success]) {
self.appDel.theNewUser = [response objectForKey:User];
self.appDel.theNewUserProfile = [response objectForKey:Profile];
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
} else {
NSLog(@"FAIL: %@", response);
[[[UIAlertView alloc] initWithTitle:@"SignUp Failed"
message:[response description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
}];
}
在您的注册方法中(或者更好的是,在您的API类中),检测nil和空值。现在的问题是在字典中插入一个nil值
下面是一个使用您的代码的示例。我检查是否有任何值长度等于零,如果是,记录一点错误
-(void)signUp:(UIButton *)sender
{
if ([[[self Email] text] length] == 0 ||
[[[self Password] text] length] == 0 ||
[[[self ConfirmPassword] text] length] == 0 ||
[[[self FirstName] text] length] == 0) {
// add an error alert here?
NSLog(@"Hold on there! You haven't filled everything in.");
return;
}
[self.api signup:self.Email.text
password:self.Password.text
confirmationPassword:self.ConfirmPassword.text
firstName:self.FirstName.text
completionHandler:^(id response, NSError *error) {
if (error) {
NSLog(@"ERROR: %@", error);
} else {
if ([[response objectForKey:Status] isEqualToString:Success]) {
self.appDel.theNewUser = [response objectForKey:User];
self.appDel.theNewUserProfile = [response objectForKey:Profile];
[self.presentingViewController dismissViewControllerAnimated:YES completion:NULL];
} else {
NSLog(@"FAIL: %@", response);
[[[UIAlertView alloc] initWithTitle:@"SignUp Failed"
message:[response description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil] show];
}
}
}];
}
请告诉我们您正在接收的崩溃消息。由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'***-[\u NSPlaceholderDictionary initWithObjects:forKeys:count::]:尝试从对象[0]插入nil对象'***第一次抛出调用堆栈:(0x15d2012 0x12dfe7e 0x1598a95 0x15c54e9 0xe116 0x7955 0x12f3705 0x22a920 0x22a8b8 0x2eb671 0x2EBCF 0x2ead38 0x25a33f 0x25a552 0x2383aa 0x229cf8 0x2634df9 0x2634ad0 0x1547bf5 0x1547962 0x1578bb6 0x1577F4 0x1577e1b 0x26337e3 0x2633668 0x22765c 0x297d 0x28A50x1)libc++abi.dylib:terminate调用抛出异常那么你认为定义新键(newuser,newuser profile)也是必需的,或者我可以使用我登录时使用的currentUser和currentUserProfile?好的,很酷。很容易修复(见下面的答案)。你能发布异常的堆栈跟踪吗?我不确定它是否发生在上面发布的代码中。谢谢,问题解决了,但是这个案例是如何处理的?我如何在屏幕上向用户显示消息?不确定为什么它不会在登录时为你崩溃,因为你没有发布这些方法。零值,比如什么是保护然而,这是您的问题。对于登录,它从json接收失败消息,如下所示:login Failed:reason=“error user/password”;status=fail;}