objective-c 目标 C 的“stringWithContentsOfURL”替代品是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2039203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the "stringWithContentsOfURL" replacement for objective C?
提问by Graeme
I found a tutorial on the net that uses the stringWithContentsOfURL command that is now deprecated as of iPhone OS 3.0. However I can't find out what I'm meant to use instead, and how to implement it.
我在网上找到了一个使用 stringWithContentsOfURL 命令的教程,该命令从 iPhone OS 3.0 开始已被弃用。但是我不知道我打算使用什么,以及如何实现它。
Below is the code surrounding the stringWithContentsOfURL line in case you need it for reference.
下面是 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:@","];
Thanks.
谢谢。
回答by bstoney
Thanks Greg, but for all those other beginners here is an example
谢谢格雷格,但对于所有其他初学者,这里是一个例子
NSError* error = nil;
NSString* text = [NSString stringWithContentsOfURL:TheUrl encoding:NSASCIIStringEncoding error:&error];
if( text )
{
NSLog(@"Text=%@", text);
}
else
{
NSLog(@"Error = %@", error);
}
回答by Greg Martin
It has been replaced with stringWithContentsOfURL:encoding:error:or stringWithContentsOfURL:usedEncoding:error:.
它已被替换为stringWithContentsOfURL:encoding:error:或stringWithContentsOfURL:usedEncoding:error:。
回答by Pravin
Below code will remove your warning message........any question please let mo know.
下面的代码将删除您的警告信息........任何问题请让我知道。
- (void) connectedToNetwork
{
BOOL aflag = ([NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.google.co.in/"] encoding:NSASCIIStringEncoding error:nil]!=NULL)?YES:NO;
if (!aflag) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Sorry!....You are not connected to network "
delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
回答by Andrew Kuklewicz
For anything more than trivial, I recommend ASIHTTPRequest:
对于不重要的事情,我推荐 ASIHTTPRequest:

