I have 2 arrays. One is a large static group of 600 objects, the other is a small varying group of 10 objects.
I want to take any common objects between the two groups and put them in a new array.
So lets say the large group contains 600 objects named 1 to 600. The smaller group contains 9 objects: 1, 2, 3, 4, 5, 6, a, b, c. I want to be able to create a new array that contains the objects 1, 2, 3, 4, 5, 6.
What is the best way to do this?
You could use NSPredicate. I hope that this post could be usefull to start...
http://marcocattai.posterous.com/nsarray-nspredicate-filter
The easiest (but not necessarily fastest (?)) way would be something like
You'll have to sort the resulting array again, however.
Are you sure that you need
NSArray
s? For intersections it would be better to useNSSet
s. For more information about the usage of NSArrays and NSSet please refer to Cocoa with Love: NSArray or NSSet, NSDictionary or NSMapTable.If you are using
NSSet
you have to create a newNSMutableSet
, which has the methodintersectSet:
, which can be used for your purpose:You can create a
NSMutableSet
from anNSArray
using theaddObjectsFromArray:
method:It may be that you can also filter the
NSArray
using thefilterUsingPredicate:
method, however I have never worked withNSPredicate
s therefore this is only an assumption.