草庐IT

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

coder 2023-09-20 原文

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

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

此外,有人可以提供一些示例代码来说明它是如何工作的吗?原始代码是用什么编码完成的(在我上面的两行中)?

我想保持兼容性,那么我应该为编码选择什么?错误是什么意思,它有什么用?

最佳答案

使用stringWithContentsOfURL:encoding:error:

编辑:这是您使用上述方法的代码:

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

您可以根据要导入的任何数据相应地更改 NSUTF8StringEncoding

关于xml - NSString stringWithContentsOfURL 已弃用。我应该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5638909/

有关xml - NSString stringWithContentsOfURL 已弃用。我应该怎么办?的更多相关文章

随机推荐