Check that the contents of one NSArray are all in another array

前端 未结 9 1765
说谎
说谎 2020-12-10 08:28

I have one NSArray with names in string objects like this:@[@\"john\", @\"smith\", @\"alex\", @\"louis\"], and I have another array that contains

相关标签:
9条回答
  • 2020-12-10 08:42

    Use NSArray filteredArrayUsingPredicate: method. Its really fast to find out similar types of object in both arrays

    NSPredicate *intersectPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", otherArray];
    NSArray *intersectArray = [firstArray filteredArrayUsingPredicate:intersectPredicate];
    

    From above code intersect array gives you same objects which are in other array.

    0 讨论(0)
  • 2020-12-10 08:42
     NSArray *array1 = [NSArray arrayWithObjects:@"a", @"u", @"b", @"v", @"c", @"f", nil];
        NSMutableArray *mainArray = [NSMutableArray arrayWithObjects:@"a", @"u", @"I", @"G", @"O", @"W",@"Z",@"C",@"T", nil];
        int j=0;
        for(int i=0; i < mainArray.count; i++)
        {
            if (j < array1.count)
            {
                for( j=0; j <= i; j++)
                {
                    if([[mainArray objectAtIndex:i] isEqualToString:[array1 objectAtIndex:j]] )
                    {
                        NSLog(@"%@",[mainArray objectAtIndex:i]);
                    }
                }
            }
    
        }
    
    0 讨论(0)
  • 2020-12-10 08:45
    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
    }
    
    0 讨论(0)
  • 2020-12-10 08:47

    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.

    0 讨论(0)
  • 2020-12-10 08:48

    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]];
    
    0 讨论(0)
  • 2020-12-10 08:52

    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
    
    0 讨论(0)
提交回复
热议问题