【问题标题】:URLWithString"http://google.com" VS URLWithString"http://apple.com"URLWithString"http://google.com" VS URLWithString"http://apple.com"
【发布时间】:2012-06-03 13:10:47
【问题描述】:

我有以下代码。

NSURL *serverUrl = [NSURL URLWithString:@"http://google.com"];
NSError *error = nil;
NSString *stringFromStream = [NSString stringWithContentsOfURL:serverUrl encoding:NSUTF8StringEncoding error:&error];
NSLog(@"string is %@ error is %@", stringFromStream, error);

这段代码产生以下错误

2012-05-29 11:57:46.914 [2221:f803] string is (null) error is Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x6a36c60 {NSURL=http://google.com, NSStringEncoding=4}

但是如果我将 URL 更改为

NSURL *serverUrl = [NSURL URLWithString:@"http://apple.com"];

我会完美地工作。

【问题讨论】:

  • 完全相同的代码在我的 mac 中工作:O

标签: objective-c nsstring nsurl


【解决方案1】:

内容的编码可能不是 UTF-8。试试这个方法:

- (id)initWithContentsOfURL:(NSURL *)url
               usedEncoding:(NSStringEncoding *)enc
                      error:(NSError **)error;

(完成后你必须releaseNSString)。

Reference.

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 2013-12-04
    • 2010-12-31
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多