什么是目标C的"stringWithContentsOfURL"替换?

Gra*_*eme 27 iphone objective-c

我在网上找到了一个使用stringWithContentsOfURL命令的教程,该命令从iPhone OS 3.0开始就被弃用了.但是我找不到我的意思,而是如何实现它.

下面是stringWithContentsOfURL行的代码,以备您参考时使用.

NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
     [addressField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:@","];
Run Code Online (Sandbox Code Playgroud)

谢谢.

bst*_*ney 81

谢谢格雷格,但对于所有其他初学者来说,这是一个例子

NSError* error = nil;
NSString* text = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
if( text )
{
    NSLog(@"Text=%@", text);
}
else 
{
    NSLog(@"Error = %@", error);
}
Run Code Online (Sandbox Code Playgroud)


Gre*_*tin 29

它已被替换为stringWithContentsOfURL:encoding:error:stringWithContentsOfURL:usedEncoding:error:.