Welcome to OGeek Q&A 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...