Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

like image 922
Tom Kelly Avatar asked Dec 07 '10 02:12

Tom Kelly


People also ask

What is difference between NSArray and NSMutableArray?

NSArray and its subclass NSMutableArray manage ordered collections of objects called arrays. NSArray creates static arrays, and NSMutableArray creates dynamic arrays. You can use arrays when you need an ordered collection of objects. NSArray is “toll-free bridged” with its Core Foundation counterpart, CFArray .

Which is faster NSArray or NSMutableArray?

NSMutableArray and NSArray both are build on CFArray , performance/complexity should be same. The access time for a value in the array is guaranteed to be at worst O(lg N) for any implementation, current and future, but will often be O(1) (constant time).

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray .

Is NSMutableArray thread safe?

In general, the collection classes (for example, NSMutableArray , NSMutableDictionary ) are not thread-safe when mutations are concerned. That is, if one or more threads are changing the same array, problems can occur.


2 Answers

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.)

like image 102
Jacob Relkin Avatar answered Oct 12 '22 21:10

Jacob Relkin


Simply like this:

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

That's quite literally, it.

like image 41
jer Avatar answered Oct 12 '22 22:10

jer