2010-12-03 5 views
1

Bonjour Quelqu'un pourrait-il me aider à mettre à jour ce snippent à iOS 4.2:stringWithContentsOfURL déprécié, aider à mettre à jour à 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];} 

La seule partie qui a besoin d'une mise à jour est ci-dessous, vous pouvez effectivly oublier le reste:

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

"stringWithContentsOfURL" a été déprécié alors quelle serait la version mise à jour?

Merci

Répondre

1

Vous devez utiliser

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

et de l'utiliser comme ça. REMPLACER

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

par

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

Branchez simplement que dans le code ci-dessus? avec le "+" ??? Merci de votre aide. –

+0

Je ne sais pas comment vous avez écrit le code ci-dessus si vous ne savez pas ce que le "+" signifie ici ... Vous devriez regarder: http://developer.apple.com/library/mac/#referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/ – gcamp

+0

excuse mon ignorance, c'est une application slapdash, comment pourrais-je mettre en œuvre ce qui précède? Je ne connais pas bien l'objectif c, j'apprends simplement à partir d'exemples. –

Questions connexes