繁体   English   中英

iPhone的Objective-C编程:尝试捕获异常

[英]objective-c iphone programming : try catch exceptions

我正在使用kumulos来访问数据库。 这是我正在谈论的代码:

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

现在,如果[theResults objectatindex:0]返回“ null”,它将每次崩溃,因此,如果用户输入数据库中未包含的内容,它将崩溃,我想抓住这个机会(NSRange感知)。

谢谢

我认为这将为您工作,而无需异常处理。

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

我假设theResults是一个NSArray (或子类)。

您检查[theResults objectAtIndex:0]是否不返回nil,或者您使用异常处理

@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.
}

我建议至少学习一点语言,或者与Google练习:-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM