Skip to content

Instantly share code, notes, and snippets.

@jdriscoll
Last active August 29, 2015 14:01
Show Gist options
  • Save jdriscoll/afeee14c5d256cce4696 to your computer and use it in GitHub Desktop.
Save jdriscoll/afeee14c5d256cce4696 to your computer and use it in GitHub Desktop.
Get differences between two NSArrays (for animating table view changes)
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