Check that the contents of one NSArray are all in

2020-02-06 03:11发布

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?

9条回答
戒情不戒烟
2楼-- · 2020-02-06 04:04

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 ur requirement.

查看更多
可以哭但决不认输i
3楼-- · 2020-02-06 04:04

Try this way;

NSArray *mainArray=@[@"A",@"B",@"C",@"D"];
NSArray *myArray=@[@"C",@"x"];

BOOL result=YES;
for(id object in myArray){
    if (![mainArray containsObject:object]) {
        result=NO;
        break;
    }
}
NSLog(@"%d",result); //1 means contains, 0 means not contains
查看更多
家丑人穷心不美
4楼-- · 2020-02-06 04:05
int num_of_matches = 0;
for(NSString *name in mainArray)
{
      if(array1 containsObject:name){
      num_of_matches++;
     }
}
if(num_of_matches == [array1 count]{
      // All objects present
}else {
      // Matched number is equal of number_of_matches
}
查看更多
登录 后发表回答