我有这段代码可以检查用户是否已经过验证,以便在应用程序上完成注册:
-(void)handleVerify:(UITapGestureRecognizer *)recognizer{
__block BOOL foundError = false;
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
UIView *transparentView = [[UIView alloc] init];
transparentView.frame = self.view.frame; //Provide frame of right side view.
transparentView.alpha = 0.5; //To provide transparent look and feel.
transparentView.backgroundColor = [UIColor blackColor];
[self.view addSubview:transparentView];
//activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.center = CGPointMake(transparentView.frame.size.width/2, transparentView.frame.size.height/2);
[transparentView addSubview:activityIndicator];
[activityIndicator startAnimating];
if(textfieldVerifyEmail.text.length > 0){
labelErrorVerfiyEmail.hidden = YES;
}else{
foundError = true;
labelErrorVerfiyEmail.hidden = NO;
}
if(textfieldVerifyNumber.text.length > 0){
labelErrorVerifyNumber.hidden = YES;
}else{
foundError = true;
labelErrorVerifyNumber.hidden = NO;
}
if(!foundError){
dispatch_queue_t concurrentQueue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 3) Load picker in background
dispatch_async(concurrentQueue2, ^{
NSString *myRequestString = [NSString stringWithFormat:@"email=%@&ver=%@", textfieldVerifyEmail.text,textfieldVerifyNumber.text];
NSString *response = [self setupPhpCall:myRequestString :@"checkUserVerification.php"];
dispatch_async(dispatch_get_main_queue(), ^{
if(response.length > 0){
NSArray *items = [response componentsSeparatedByString:@"|~|"];
NSString *ver =[items objectAtIndex:0];
NSString *idUser =[items objectAtIndex:1];
if([ver isEqualToString:@"verified"]){
textfieldVerifyNumber.text = @"";
textfieldVerifyEmail.text = @"";
[activityIndicator stopAnimating];
[transparentView removeFromSuperview];
[activityIndicator removeFromSuperview];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"REGISTERED" message:@"You are now Registered" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil, nil];
[alert show];
userID = [idUser intValue];
[viewCreds removeFromSuperview];
[viewCredsRegister removeFromSuperview];
[viewCredsSignin removeFromSuperview];
[viewCredsVerify removeFromSuperview];
[self setupHeader];
[self setupNavigation];
[self setupFooter];
[self setupFooterNavigation];
[self switchView:7];
[self switchView:1];
}else{
[activityIndicator stopAnimating];
[transparentView removeFromSuperview];
[activityIndicator removeFromSuperview];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"VERIFICATION INVALID" message:@"Our records show that the Verification Number is incorrect, please try again or contact support@hugt.co.uk" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil, nil];
[alert show];
}
}else{
[activityIndicator stopAnimating];
[transparentView removeFromSuperview];
[activityIndicator removeFromSuperview];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"SERVER ERROR" message:@"Server not Responding, please try back later..." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil, nil];
[alert show];
}
});
});
}else{
[activityIndicator stopAnimating];
[transparentView removeFromSuperview];
[activityIndicator removeFromSuperview];
}
}
但是一旦点击验证按钮,我就会收到此错误:
[1649:65950] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'
*** First throw call stack:
(
0 CoreFoundation 0x00e2c746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x006a2a97 objc_exception_throw + 44
2 CoreFoundation 0x00d0dcb2 -[__NSArrayI objectAtIndex:] + 210
3 InstantForum 0x000944ab __31-[ViewController handleVerify:]_block_invoke_2 + 203
4 libdispatch.dylib 0x052fc5ea _dispatch_call_block_and_release + 15
5 libdispatch.dylib 0x0531ebef _dispatch_client_callout + 14
6 libdispatch.dylib 0x053046bb _dispatch_main_queue_callback_4CF + 993
7 CoreFoundation 0x00d858ee __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
8 CoreFoundation 0x00d435f0 __CFRunLoopRun + 2256
9 CoreFoundation 0x00d42a5b CFRunLoopRunSpecific + 443
10 CoreFoundation 0x00d4288b CFRunLoopRunInMode + 123
11 GraphicsServices 0x047682c9 GSEventRunModal + 192
12 GraphicsServices 0x04768106 GSEventRun + 104
13 UIKit 0x013ff0b6 UIApplicationMain + 1526
14 InstantForum 0x00112e9a main + 138
15 libdyld.dylib 0x05349ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(Recorded Frame)
此行代码出现错误:
dispatch_async(dispatch_get_main_queue(), ^{
答案 0 :(得分:3)
在致电NSArray *items = [response componentsSeparatedByString:@"|~|"];
之前检查-objectAtIndex
的计数。
您可能未能-componentsSeparatedByString
。你确定@"|~|"
在字符串中吗?