Last active
August 29, 2015 14:01
-
-
Save jdriscoll/afeee14c5d256cce4696 to your computer and use it in GitHub Desktop.
Get differences between two NSArrays (for animating table view changes)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void arrayDiff(NSArray *array1, NSArray *array2, NSSet **objectsToAdd, NSSet **objectsToRemove, NSSet **objectsInBoth) { | |
if (array1 == nil) { | |
*objectsToAdd = [NSSet setWithArray:array2]; | |
*objectsToRemove = [NSSet set]; | |
*objectsInBoth = [NSSet set]; | |
} | |
else if (array2 == nil) { | |
*objectsToAdd = [NSSet set]; | |
*objectsToRemove = [NSSet setWithArray:array1]; | |
*objectsInBoth = [NSSet set]; | |
} | |
else { | |
NSMutableArray *toAdd = [array2 mutableCopy]; | |
[toAdd removeObjectsInArray:array1]; | |
*objectsToAdd = [NSSet setWithArray:toAdd]; | |
NSMutableArray *toRemove = [array1 mutableCopy]; | |
[toRemove removeObjectsInArray:array2]; | |
*objectsToRemove = [NSSet setWithArray:toRemove]; | |
NSMutableArray *inBoth = [array2 mutableCopy]; | |
[inBoth removeObjectsInArray:toAdd]; | |
*objectsInBoth = [NSSet setWithArray:inBoth]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment