ios – NSArray中的NSMutableArray

前端之家收集整理的这篇文章主要介绍了ios – NSArray中的NSMutableArray前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有以下代码,我想使用NSMutable数组,而不是NSArray
你可以告诉我如何加载NSMutable数组,因为当前的方法不起作用.
  1. -(void) whatever{
  2. NSData *htmlData = [[NSString stringWithContentsOfURL:[NSURL URLWithString: @"http://www.objectgraph.com/contact.html"]] dataUsingEncoding:NSUTF8StringEncoding];
  3. TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
  4. NSArray *titles = [xpathParser search:@"//h3"]; // get the page title - this is xpath notation
  5. TFHppleElement *title = [titles objectAtIndex:0];
  6. NSString *myTitles = [title content];
  7.  
  8. NSArray *articles = [xpathParser search:@"//h4"]; // get the page article - this is xpath notation
  9. TFHppleElement *article = [articles objectAtIndex:0];
  10. NSString *myArtical = [article content];

我努力了 :

  1. NSMutableArray *titles = [xpathParser search:@"//h3"];

但它确实加载值?

解决方法

您可以在NSArray对象上调用mutableCopy以返回给您一个NSMutableArray.

请注意,被调用者将获得该对象的所有权,因为方法名称包含“副本”.

(Apple’s memory management guide指出,包含“alloc”,“new”或“copy”这个词的方法名称应该按照惯例返回一个您拥有的对象,因此必须放弃在某一时刻的所有权.

猜你在找的iOS相关文章