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.
+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
2009-06-25 23:28:24
+3
A:
NSArray* uniqueArray = [[NSSet setWithArray:originalArray] allObjects];
Uniqueness is based on the isEqual: method.
Peter N Lewis
2009-06-26 02:12:41
Shouldn't that be NSArray *uniqueArray = [[NSSet withWithArray:originalArray] allObjects];
willc2
2009-06-26 04:37:05
+1 for answering with code and not a link.
willc2
2009-06-26 04:37:54