Search code examples
iphoneobjective-cexceptiontry-catchnsrange

objective-c iphone programming : try catch exceptions


im using kumulos to have access to a database. this is the code i am talking about :

NSString *location = [[theResults objectAtIndex:0] objectForKey:@"location"];

Now the thing if [theResults objectatindex:0] return "null" it crash everytime so if the user enter something that is not in the database it crash i want to catch this exeption (NSRange exeception).

Thanks


Solution

  • I think this will work for you without requiring exception handling.

    if ([theResults count] > 0) {
        NSString *location = [[theResults objectAtIndex:0] objectForKey:@"location"];
    }
    

    I'm assuming that theResults is an NSArray (or subclass).