NSMutableArray from NSArray

Tom Kelly picture Tom Kelly · Dec 7, 2010 · Viewed 25.5k times · Source

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.

-(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?

Answer

Jacob Relkin picture Jacob Relkin · Dec 7, 2010

You can invoke mutableCopy on an NSArray object to return to you an NSMutableArray.

Note that the callee will obtain ownership of this object since the method name contains "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.)