Search code examples
iosnsarray

How to find If array contains two or more objects


I'm attempting to implement the containsObject but with two or more parameters, is this possible?

Currently I've got:

and apparently there's too many arguments. I've delved through Apple's docs but I'm yet to find anything. Any suggestions?

if ([ myArray containsObject:@"1", @"2"]){
    NSLog(@"if");
} else if([ myArray containsObject:@"1", @"2",@"3",@"4"]) {
   NSLog(@"else if");
}else if([ myArray containsObject:@"1", @"2",@"3"]) {
   NSLog(@"else");
}

myArray:-

myArray is (
    1,
    2,
    3,
    4
)

Solution

  • you can check subsets of array

     NSArray *arry1= [NSArray arrayWithObjects:@"1",@"2",@"3",@"4", nil];
            NSArray *arry2= [NSArray arrayWithObjects:@"1",@"2", nil];
    
            NSSet *set1 = [NSSet setWithArray:arry1];
            NSSet *set2 = [NSSet setWithArray:arry2];
    
            if ([set2 isSubsetOfSet:set1])
            {
                NSLog(@"array1 contains all elements of array 2");
            }else{
                NSLog(@"array1 does not contains all elements of array 2");
            }