stringWithContentsOfURL折旧了,有助于更新到4.2吗?

时间:2010-12-03 13:19:37

标签: ios4 nsstring nsdata

您好 任何人都可以帮我更新这个片段到iOS 4.2:

-(void) whatever{
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
NSArray *elements  = [xpathParser search:@"//h3"]; // get the page title - this is xpath notation
TFHppleElement *element = [elements objectAtIndex:0];
NSString *myTitle = [element content];
NSLog(myTitle);
[xpathParser release];
[htmlData release];}

需要更新的唯一部分如下,您可以有效地忘记其余部分:

NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:  @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];

“stringWithContentsOfURL”已被删除,那么更新版本会是什么?

由于

1 个答案:

答案 0 :(得分:1)

你应该使用

+ (id)stringWithContentsOfURL:(NSURL *)url encoding:(NSStringEncoding)enc error:(NSError **)error

并使用它。替换

NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:  @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];

通过

NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString:  @"http://www.objectgraph.com/contact.html"]] encoding:NSUTF8StringEncoding error:nil];