iOS 8中的NSError域检查条件失败[重复]

时间:2020-12-01 00:18:45

This question already has an answer here:

这个问题在这里已有答案:

I am using WKWebView for iOS 8 devices and want to check if the error occuring is NSURLErrorDomain then I want to make some changes, until now, I have added below code, but somehow the compiler goes into else part, now sure why.

我正在使用WKWebView for iOS 8设备,并想检查是否发生错误是NSURLErrorDomain然后我想做一些更改,直到现在,我已经添加了下面的代码,但不知何故编译器进入其他部分,现在确定原因。

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    if ( [error domain] == NSURLErrorDomain )
    {
        //code here
    }
    else
    {
        //But compiler always comes here
    }
 }

The error I do get is 'NSURLErrorDomain', but compiler is not executing if loop and goes in else part. Can anyone tell me why it is?

我得到的错误是'NSURLErrorDomain',但编译器没有执行if循环并进入其他部分。谁能告诉我它为什么会这样?

2 个解决方案

#1


7  

The domain property is an NSString, so you need to compare it like this:

domain属性是一个NSString,所以你需要比较它:

if ([[error domain] isEqualToString:NSURLErrorDomain]) {

To give an idea of how the developers at Apple would handle this, check the code below which is copied from the Error Handling Programming Guide, listing 2-3:

要了解Apple的开发人员如何处理此问题,请检查下面的代码,该代码是从错误处理编程指南中复制的,清单2-3:

NSString *errorMsg;
if ([[error domain] isEqualToString:NSURLErrorDomain]) {
    switch ([error code]) {
        case NSURLErrorCannotFindHost:
            errorMsg = NSLocalizedString(@"Cannot find specified host. Retype URL.", nil);
            break;
        case NSURLErrorCannotConnectToHost:
            errorMsg = NSLocalizedString(@"Cannot connect to specified host. Server may be down.", nil);
            break;
        case NSURLErrorNotConnectedToInternet:
            errorMsg = NSLocalizedString(@"Cannot connect to the internet. Service may not be available.", nil);
            break;
        default:
            errorMsg = [error localizedDescription];
            break;
    }
} else {
    errorMsg = [error localizedDescription];
}

#2


0  

According to the Apple Documentation the domain value will be @"NSURLErrorDomain" with an error code defined by the enum starting with NSURLErrorXxx.

根据Apple文档,域值将为@“NSURLErrorDomain”,其错误代码由以NSURLErrorXxx开头的枚举定义。

Therefore you want:

因此你想要:

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    if ([[error domain] isEqualToString:@"NSURLErrorDomain"] &&
        [error code] == NSURLErrorTimedOut )    // for example
    {
        // Handle timeout
    }
    else
    {
        // Some other error
    }
 }

#1


7  

The domain property is an NSString, so you need to compare it like this:

domain属性是一个NSString,所以你需要比较它:

if ([[error domain] isEqualToString:NSURLErrorDomain]) {

To give an idea of how the developers at Apple would handle this, check the code below which is copied from the Error Handling Programming Guide, listing 2-3:

要了解Apple的开发人员如何处理此问题,请检查下面的代码,该代码是从错误处理编程指南中复制的,清单2-3:

NSString *errorMsg;
if ([[error domain] isEqualToString:NSURLErrorDomain]) {
    switch ([error code]) {
        case NSURLErrorCannotFindHost:
            errorMsg = NSLocalizedString(@"Cannot find specified host. Retype URL.", nil);
            break;
        case NSURLErrorCannotConnectToHost:
            errorMsg = NSLocalizedString(@"Cannot connect to specified host. Server may be down.", nil);
            break;
        case NSURLErrorNotConnectedToInternet:
            errorMsg = NSLocalizedString(@"Cannot connect to the internet. Service may not be available.", nil);
            break;
        default:
            errorMsg = [error localizedDescription];
            break;
    }
} else {
    errorMsg = [error localizedDescription];
}

#2


0  

According to the Apple Documentation the domain value will be @"NSURLErrorDomain" with an error code defined by the enum starting with NSURLErrorXxx.

根据Apple文档,域值将为@“NSURLErrorDomain”,其错误代码由以NSURLErrorXxx开头的枚举定义。

Therefore you want:

因此你想要:

- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error
{
    if ([[error domain] isEqualToString:@"NSURLErrorDomain"] &&
        [error code] == NSURLErrorTimedOut )    // for example
    {
        // Handle timeout
    }
    else
    {
        // Some other error
    }
 }