Are you sure that you need NSArray
s? For intersections it would be better to use NSSet
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 new NSMutableSet
, which has the method intersectSet:
, which can be used for your purpose:
NSMutableSet *set1 = [[NSMutableSet alloc] initWithObjects:@"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", nil];
NSMutableSet *set2 = [[NSMutableSet alloc] initWithObjects:@"2", @"4", @"6", @"8", @"10", @"12", @"14", @"18", nil];
NSLog(@"set1: %@", set1);
NSLog(@"set2: %@", set2);
[set1 intersectSet:set2];
NSLog(@"isec: %@", set1);
You can create a NSMutableSet
from an NSArray
using the addObjectsFromArray:
method:
NSArray *array = [[NSArray alloc] initWithObjects:@"1", @"2", nil];
NSMutableSet *set = [[NSMutableSet alloc] init];
[set addObjectsFromArray:array];
It may be that you can also filter the NSArray
using the filterUsingPredicate:
method, however I have never worked with NSPredicate
s therefore this is only an assumption.