I\'m new ios developer, I want to compare and change attributes
Array1 = (object1,object2, object3, object4) Array2 = (object2,object4, object5, object8)
Compare
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]
I think you'll have to do a n*n
search in this case. Loop through every object in Array1, have a nested loop and compare every item in Array2 to the current object (in Array1). If they are equal then change your attribute.
for (int i = 0; i < [array1 count]; i++)
for (int j = 0; j < [array2 count]; j++)
if ([array1 objectAtIndex:i] == [array2 objectAtIndex:j]) {
// do yo thangs
}
You can use fast enumeration to pass through array 2, then use containsObject:
to check if it belongs to array1:
for (id object in array2)
{
if ([array1 containsObject:object])
{
// change your settings here
}
You could also create a new array using filteredArrayUsingPredicate:
, or get the index paths of the matching objects using indexesOfObjectsPassingTest:
. You haven't said how many objects are likely to be in your array so I don't know if performance is going to be an issue.