开发者

stringWithContentsOfURL depreciated, help to update to 4.2?

开发者 https://www.devze.com 2023-01-29 04:40 出处:网络
Hello Could anyone help me update this snippent to iOS 4.2: -(void) whatever{ NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @\"http://www.objectgraph.com/contact.html\"]

Hello Could anyone help me update this snippent to iOS 4.2:

-(void) whatever{
NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlDa开发者_运维百科ta];
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];}

The only part that needs updating is below, you can effectivly forget the rest:

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

"stringWithContentsOfURL" has been deprechiated so what would be the updated version?

Thanks


You should use

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

And use it like that. Replace

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

by

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

精彩评论

暂无评论...
验证码 换一张
取 消