I have one NSArray
with names in string objects like this:@[@"john", @"smith", @"alex",
@"louis"]
, and I have another array that contains lots of names. How can I check that all the objects in the first array are in the second?
NSSet
has the functionality that you are looking for.
If we disregard performance issues for a moment, then the following snippet will do what you need in a single line of code:
BOOL isSubset = [[NSSet setWithArray: array1] isSubsetOfSet: [NSSet setWithArray: mainArray]];
Use this code..
NSArray *temp1 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Jac", nil];
NSArray *temp2 = [NSArray arrayWithObjects:@"john",@"smith",@"alex",@"loui,@"Rob", nil];
NSMutableSet *telephoneSet = [[NSMutableSet alloc] initWithArray:temp1] ;
NSMutableSet *telephoneSet2 = [[NSMutableSet alloc] initWithArray:temp2];
[telephoneSet intersectSet:telephoneSet2];
NSArray *outPut = [telephoneSet allObjects];
NSLog(@"%@",outPut);
output array contains:
"john","smith","alex","loui
as per your requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With