【问题标题】:Handling NSError with error.code使用 error.code 处理 NSError
【发布时间】:2015-04-27 23:31:36
【问题描述】:

我正在尝试用UIAlertView 处理特定的NSErrors,但是当调用webview didFailLoadWithError 时,没有调用if 语句中的任何代码。下面是我的条件语句:

 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
loadFailedBool = YES;
NSLog(@"Error code %ld", (long)[error code]);
if ([error code] != -999) {
    //loading was cancelled and another one initiated. Don't show alert
    return;
}
else if ([error code] == NSURLErrorTimedOut || [error code] == kCFURLErrorTimedOut) {
    //connection timed out
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Network Connection timed out" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    alert.tag = 1;
}
else if ([error code] == -1005) {
    //connection lost
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Network Connection to the host is lost" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    alert.tag = 1;
}
else if ([error code] == kCFURLErrorNotConnectedToInternet || [error code] == -1009) {
    //no internet connection
    NSLog(@"Error here");
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Looks like you are not connected to the internet" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
    [alert show];
    alert.tag = 1;
}
else if ([error.domain isEqualToString:@"WebKitErrorDomain"] && error.code == 102) {

    return;
  }
}

我的问题是,我如何调用条件,因为有时在没有互联网的情况下日志会打印 -1009。谢谢

【问题讨论】:

  • [error code] == kCFURLErrorNotConnectedToInternet || [error code] == -1009是没有意义的,因为kCFURLErrorNotConnectedToInternet -1009

标签: ios error-handling conditional nserror


【解决方案1】:

问题是你永远不会越过这条线:

if ([error code] != -999) {
    return;
}

所以如果错误代码是-1009,或者任何东西-999,那就太晚了——你已经说过return,整个事情就结束了。后面的代码不会执行。

【讨论】:

  • 当你开始犯微不足道的错误时,你就知道该打瞌睡了。谢谢马特。我想我需要休息一下。
猜你喜欢
  • 1970-01-01
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2014-01-26
相关资源
最近更新 更多