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

发布于 2024-10-25 22:39:31 字数 257 浏览 8 评论 0原文

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

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

现在如果 [theResults objectatindex:0] 返回“null”,它每次都会崩溃 因此,如果用户输入数据库中没有的内容,则会崩溃,我想捕获此异常(NSRange 异常)。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又爬满兰若 2024-11-01 22:39:31

我认为这对你有用,不需要异常处理。

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

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

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

我只土不豪 2024-11-01 22:39:31

要么检查 [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 进行练习:-)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文