NSString stringWithContentsOfURL已弃用。 我该怎么办?

我写了一些使用stringWithContentsOfURL代码,但事实certificate,它现在已经被弃用了。 你能帮我replace吗? 这是我的代码:

 NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]]; NSArray *listItems = [locationString componentsSeparatedByString:@","]; 

另外,有人可以有一些示例代码来显示它是如何工作的? 什么编码是在(我的2行上面)完成的原始代码?

我想保持兼容性,所以我应该select什么编码? 错误是什么意思,它用于什么?

使用stringWithContentsOfURL:encoding:error:

编辑:这是你的代码使用上面的方法:

 NSError *error = nil; NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:&error]; NSArray *listItems = [locationString componentsSeparatedByString:@","]; 

您可以相应地更改NSUTF8StringEncoding ,以查看您导入的任何数据。

NSString引用说这个:

使用stringWithContentsOfURL:encoding:error:stringWithContentsOfURL:usedEncoding:error: instead。