iphonecocoansstring

NSCocoaErrorDomain Code=256


I have been stuck with this for a while and don't seem to get around this.

I am trying to read the contents of an URL as a string from an URL, But i get a weird

Error -> Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)"

My code :

fetchedString = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"www.example.com/iphone"] encoding:NSUTF8StringEncoding error:&error];                                     
    NSLog(@"%@",fetchedString);

    // if there is something wrong with the URL 
    if (error) {
        NSLog(@"Error -> %@", error);
        return ;
    }

What am I doing wrong? I tried using getting as NSData as well, but I get null back.


Solution

  • Yes, the URL is missing the scheme: "http://".

    "Error -> Error Domain=NSCocoaErrorDomain Code=256"

    For the error code check the Apple documentation:

    NSError codes in the Cocoa error domain.

    NSFileReadUnknownError = 256,
    

    NSFileReadUnknownError
    "Read error, reason unknown"

    Not that the error definition is very helpful. :-)

    Also do not check if error is nil to determine if there is an error, check the return value for nil. error is not guaranteed to be nil on successful execution.