Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCocoaErrorDomain Code=256

Tags:

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.

like image 444
Neelesh Avatar asked Apr 11 '12 12:04

Neelesh


1 Answers

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.

like image 82
zaph Avatar answered Sep 19 '22 22:09

zaph