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

前端 未结 9 1766
说谎
说谎 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:57

    You can use the concept of [NSArray containsObject:], where your objects will be from your array1 like you say "john","smith","alex","loui"

    0 讨论(0)
  • 2020-12-10 09:01

    Run a loop and use isEqualToStiring to verify whether array1 objects exists in mainArray.

    0 讨论(0)
  • 2020-12-10 09:01

    If you just need to check if all objects from array1 are in mainArray, you should just use NSSet e.g.

    BOOL isSubset = [[NSSet setWithArray:array1] isSubsetOfSet:[NSSet setWithArray:mainArray]] 
    

    if you need to check which objects are in mainArray, you should take a look at NSMutableSet

    NSMutableSet *array1Set = [NSMutableSet setWithArray:array1];
    [array1Set intersectSet:[NSSet setWithArray:mainArray]];
    //Now array1Set contains only objects which are present in mainArray too
    
    0 讨论(0)
提交回复
热议问题