NSCocoaErrorDomain 代码=256 [英] NSCocoaErrorDomain Code=256

查看:39
本文介绍了NSCocoaErrorDomain 代码=256的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经坚持了一段时间,似乎没有解决这个问题.

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

我正在尝试将 URL 的内容作为 URL 中的字符串读取,但我得到一个奇怪的

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 操作无法完成.(Cocoa 错误 256.)"

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

我的代码:

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 ;
    }

我做错了什么?我也尝试使用作为 NSData 获取,但我得到了空值.

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

推荐答案

是的,URL 缺少方案:http://".

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

错误 -> 错误域=NSCocoaErrorDomain 代码=256"

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

对于错误代码,请检查 Apple 文档:

For the error code check the Apple documentation:

Cocoa 错误域中的 NSError 代码.

NSError codes in the Cocoa error domain.

NSFileReadUnknownError = 256,

NSFileReadUnknownError
读取错误,原因未知"

NSFileReadUnknownError
"Read error, reason unknown"

并不是说错误定义很有帮助.:-)

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

也不要检查error是否为nil来判断是否有错误,检查nil的返回值.error 不保证成功执行时nil.

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.

这篇关于NSCocoaErrorDomain 代码=256的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆