iPhone: Error and error handling confusion in comparison.

Posted by Mr. McPepperNuts on Stack Overflow See other posts from Stack Overflow or by Mr. McPepperNuts
Published on 2010-06-09T18:22:32Z Indexed on 2010/06/09 18:42 UTC
Read the original article Hit count: 245

Filed under:
|
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];

if(results == nil){
    NSLog(@"No results found");
    searchObj = nil;
}else{
    if ([[[results objectAtIndex:0] name] caseInsensitiveCompare:passdTextToSearchFor] == 0) {
        NSLog(@"results %@", [[results objectAtIndex:0] name]);
        searchObj = [results objectAtIndex:0];
    }else {
        NSLog(@"No results found");
        searchObj = nil;
    }       
}

The code above compares data a user enters to data pulled from a database. If I enter data which is in the database; it works. But if I enter complete gibberish it returns the error below instead of "No results found."

*** WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: <NSRangeException> *** -[NSCFArray objectAtIndex:]: index (0) beyond bounds (0)

The results array being null should be accounted for during the checks in the above code, no?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c