How to compare arrays? And change attributes?

前端 未结 3 951
忘掉有多难
忘掉有多难 2021-01-21 19:32

I\'m new ios developer, I want to compare and change attributes

Array1 = (object1,object2, object3, object4) Array2 = (object2,object4, object5, object8)

Compare

3条回答
  •  长发绾君心
    2021-01-21 20:25

    You can use sets for this

    NSMutableSet *array1Set = [NSMutableSet setWithArray:array1];
    NSSet *array2Set = [NSSet setWithArray:array2];
    [array1Set intersectSet:array2Set];
    

    You now have a set with just the objects which are in both arrays. Now you can use enumerateObjectsUsingBlock: on the set to manipulate the objects or convert the set back to an array NSArray *filteredArray = [array1Set allObjects]

提交回复
热议问题