简体   繁体   English

来自NSArray的NSMutableArray

[英]NSMutableArray from NSArray

i have the following code and i want to use NSMutable arrays instead of NSArray could you tell me how to load the NSMutable array, as the current method does not work. 我有以下代码,我想使用NSMutable数组而不是NSArray你能告诉我如何加载NSMutable数组,因为当前的方法不起作用。

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

NSArray *articles  = [xpathParser search:@"//h4"]; // get the page article - this is xpath     notation
TFHppleElement *article = [articles objectAtIndex:0];
NSString *myArtical = [article content];

i have tried : 我努力了 :

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

but it does load the values? 但它确实加载了这些值?

You can invoke mutableCopy on an NSArray object to return to you an NSMutableArray . 您可以在NSArray对象上调用mutableCopy以返回NSMutableArray

Note that the callee will obtain ownership of this object since the method name contains "copy". 请注意,被调用者将获得此对象的所有权,因为方法名称包含“copy”。

( Apple's memory management guide states that a method name containing the words "alloc", "new" or "copy" should by convention return an object which you own, and as such must relinquish ownership of at some point.) Apple的内存管理指南声明,包含单词“alloc”,“new”或“copy”的方法名称应该按照惯例返回您拥有的对象,因此必须放弃在某些时候的所有权。)

Simply like this: 就像这样:

NSArray* someArray = [xpathParser search:@"//h3"];
NSMutableArray* mutableArray = [someArray mutableCopy];

That's quite literally, it. 这完全是字面意思。

假设[xpathparser ...]返回NSArray ,您可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM