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).
开发者_如何学PythonThanks
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).
either you check that [theResults objectAtIndex:0] does not return nil, or you use exception handling
@try {
NSString *location = [[theResults objectAtIndex:0] objectForKey:@"location"];
}
@catch (NSRangeEception * e) {
NSLog(@"catching %@ reason %@", [e name], [e reason]);
}
@finally {
//something that you want to do wether the exception is thrown or not.
}
I would suggest to study the language at least a little bit, though, or practice with google :-)
精彩评论