问题描述
我编写了一些使用 stringWithContentsOfURL
的代码,但事实证明它已被弃用。你能帮我换一下吗?这是我的代码:
I've written some code that uses stringWithContentsOfURL
, but it turns out that it is now deprecated. Can you help me replace it? Here's my code:
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
此外,有人可以提供一些示例代码来说明它的工作原理吗?原始代码的编码是什么(在上面的2行中)?
Additionally, can someone have some sample code to show how it works? What encoding was the original code done in (in my 2 lines above)?
我想保持兼容性,那么我应该为编码选择什么?错误意味着什么以及它用于什么?
I want to maintain compatibility so what should I choose for the encoding? What does error mean and what is it used for?
推荐答案
使用 stringWithContentsOfURL:encoding:error:
编辑:这是使用上述方法的代码:
Here's your code using the above method:
NSError *error = nil;
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:&error];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
您可以相应地更改 NSUTF8StringEncoding
以获取任何数据你正在进口。
You can change NSUTF8StringEncoding
accordingly for whatever data you're importing.
这篇关于不推荐使用NSString stringWithContentsOfURL。我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!