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

前端 未结 9 1791
说谎
说谎 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 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
    

提交回复
热议问题