Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return an NSMutableArray from an NSSet

I'm able to put the contents of an NSSet into an NSMutableArray like this:

NSMutableArray *array = [set allObjects]; 

The compiler complains though because [set allObjects] returns an NSArray not an NSMutableArray. How should this be fixed?

like image 252
node ninja Avatar asked Sep 30 '10 00:09

node ninja


People also ask

How do you reverse an NSArray?

You can reverse a NSArray by writing your own loop iterating from the end towards the beginning and using a second array to add the items in reverse order. Or you can simply use - (NSEnumerator *)reverseObjectEnumerator from the NSArray class.

Is it faster to iterate through an NSArray or an NSSet?

Yes, NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating.

Does NSDictionary retain objects?

An NSDictionary will retain it's objects, and copy it's keys.


1 Answers

Since -allObjects returns an array, you can create a mutable version with:

NSMutableArray *array = [NSMutableArray arrayWithArray:[set allObjects]]; 

Or, alternatively, if you want to handle the object ownership:

NSMutableArray *array = [[set allObjects] mutableCopy]; 
like image 136
dreamlax Avatar answered Sep 26 '22 00:09

dreamlax