Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

iphone - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...