0

I have this code that checks when a user is Verified to to complete Registration on the App:

-(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 [email protected]" 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];
    }
}

But I get this error as soon as I click the verify button:

[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) 

The Error occurs on this line of code:

dispatch_async(dispatch_get_main_queue(), ^{
14
  • 7
    How tall is your monitor? Commented Jul 12, 2015 at 22:41
  • 3
    Fixed formatting. My mother always told me I was a good person, and this proves it! But that's some gnarly code. I suggest you edit this question, replacing it with an MRE. Commented Jul 12, 2015 at 22:52
  • 1
    check the count of your NSArray *items = [response componentsSeparatedByString:@"|~|"]; before calling -objectAtIndex Commented Jul 12, 2015 at 22:55
  • 3
    As soon as you get this code working, I highly recommend you post it to Code Review where people will shorter monitors will help you learn how to organize your code better. Commented Jul 12, 2015 at 22:57
  • 1
    I think @0yeoj has it right. someArray[0] is dangerous unless you've concluded that someArray.count > 0 Commented Jul 12, 2015 at 22:58

1 Answer 1

3

Check the count of your NSArray *items = [response componentsSeparatedByString:@"|~|"]; before calling -objectAtIndex.

You might have failed to -componentsSeparatedByString. Are you sure @"|~|" is in the string?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.