开发者

Cocoa - NSError description coming from nowhere

I have this piece of code :

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response 
{
    if ([response respondsToSelector:@selector(statusCode)]) {

        int statusCode = [((NSHTT开发者_高级运维PURLResponse*)response) statusCode];
        if (statusCode >= 400) {
            NSError* statusError = [NSError errorWithDomain:@"Server connection error" code:statusCode userInfo:nil];
            [self connection:connection didFailWithError:statusError];
        }
    }
}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{
NSLog(@"%@", [error localizedDescription]);
}

That gives for a missing page :

--> The operation couldn’t be completed. (Server connection error error 404.)

From where does that description (localized or not) comes from ?

I've just initialized the NSError with a code and a custom meaningless domain string...


That error message means that your online resource cannot be found by the server.

For example: http://www.google.com/notthepageyourelookingfor.

HTTP 404 - Wikipedia

If you're asking where the error message originates, it ought to break down like this:

  • localizedDescription:

    The operation couldn't be completed ()

By default this method returns the object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code. NSLocalizedDescriptionKey is a localized string representation of the error that, if present, will be returned by localizedDescription. Available in Mac OS X v10.2 and later. Declared in NSError.h.

  • errWithDomain:@"Server connection error":

    Server connection error

  • code:statusCode:

    error 404


The operation couldn’t be completed.

That is a standard POSIX error. Your domain and error code are just appended to the actual error message to determine the error's origin. Usually, a reverse-DNS style domain is used, like com.developer.package.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜