views:

99

answers:

3

I want an array which contains only unique items. I know I could do this with an NSDictionary adding items with keys and then get allKeys. This would ensure that the NSArray contains only unique items, but I feel that this would be overkill and believe that there should be an easier way to do this, but cannot find one.

+1  A: 

You can use NSSet for this

Jason Coco
+2  A: 

Use NSSet or NSMutableSet for this. Keep in mind that uniqueness will be based on object address if you don't override the isEqual: method. Unless, of course, you are using classes that implement that method (NSNumber, NSValue, for example).

Marco Mustapic
+3  A: 
NSArray* uniqueArray = [[NSSet setWithArray:originalArray] allObjects];

Uniqueness is based on the isEqual: method.

Peter N Lewis
Shouldn't that be NSArray *uniqueArray = [[NSSet withWithArray:originalArray] allObjects];
willc2
+1 for answering with code and not a link.
willc2