传递错误委托
我正在尝试将某种错误处理合并到 iOS 应用程序中。在了解到 try/catch 块并不是真正的好习惯之后,我现在正在查看 NSURLConnection 的 &error 输出参数。我试图传递一个对 NSError 的引用,然后我继续通过链向下传递它。
第一个函数调用我的 DAL 中的一个方法:
NSError *error = nil;
SearchResult *res = [db getHereditaments:&error];
if (error) {
[self performSelectorOnMainThread:@selector(DisplayError:) withObject:error waitUntilDone:NO];
//todo: handle error
} else {
[self performSelectorOnMainThread:@selector(DisplayResults:) withObject:res waitUntilDone:NO];
}
然后第二个方法(被调用)尝试传递错误。这个工作得很好,直到我添加了对 NSError 是否为 nil 的检查,并且返回了这种情况下的数据,那么为什么还返回错误呢?
调用的方法是:
-(SearchResult *) getData:(NSError**)error {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userName = [prefs stringForKey:UsernameKey];
NSString * password = [prefs stringForKey:PasswordKey];
NSURL *url = [UrlUtility getSearchResults:userName passwordHash:password];
CXMLDocument *responseObj = [UrlUtility xmlDocWithUrl:url onError:&error];
if(error ==nil){// should be nil if the connection was successful shouldn't it?
I'm trying to incorporate some sort of error handling into an iOS app. After reading that try/catch blocks aren't really good practice, I'm now looking at the &error output parameter for NSURLConnection. I'm trying to pass in a reference to an NSError, then I keep passing it down through the chain.
The first function calls a method in my DAL:
NSError *error = nil;
SearchResult *res = [db getHereditaments:&error];
if (error) {
[self performSelectorOnMainThread:@selector(DisplayError:) withObject:error waitUntilDone:NO];
//todo: handle error
} else {
[self performSelectorOnMainThread:@selector(DisplayResults:) withObject:res waitUntilDone:NO];
}
Then the second method (which is called) tries to pass down the error. This worked fine until I added the check for whether the NSError was nil, and data in this case is returned, so why is an error also returned?
The method called is:
-(SearchResult *) getData:(NSError**)error {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userName = [prefs stringForKey:UsernameKey];
NSString * password = [prefs stringForKey:PasswordKey];
NSURL *url = [UrlUtility getSearchResults:userName passwordHash:password];
CXMLDocument *responseObj = [UrlUtility xmlDocWithUrl:url onError:&error];
if(error ==nil){// should be nil if the connection was successful shouldn't it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,首先,如果您要在链中传递相同的错误,则需要将 &*error 传递到方法中。
其次,如果您处理错误指针,您通常希望处理 *error 而不是 error。
第三,你永远不应该检查 error == nil 是否。您应该检查responseObj == nil 是否。如果这是真的,那么您可以使用该错误,如果不是,那么您不应该有错误。
最后,与主题不完全相关,但应该对您作为开发人员有所帮助,您不应该在方法名称的开头使用“get”。 Get 仅用于通过引用返回各种值,您通常应该避免这样做。您的方法可能应该类似于 downloadSearchResultsWithError: 和 URLForSearchResultsWithUsername:password:
Ok, first of all if you're passing the same error down the chain you need to pass &*error into the method.
Secondly if you deal with the error pointer at all you generally want to deal with *error rather than error.
And thirdly you should NEVER check if error == nil. You should be checking if responseObj == nil. If that is true then you can use the error, if not then you should not have an error.
Lastly, not completely related to the subject but should help you as a dev, you shouldn't really use "get" at the start of method names. Get is only ever used for returning various values by reference, which you should generally avoid. Your methods should probably be something like downloadSearchResultsWithError: and URLForSearchResultsWithUsername:password: