ArrayDiff
Yet another diff calculation utility, efficient & tested.
Install / Use
/learn @nickynick/ArrayDiffREADME
ArrayDiff
You might very well be familiar with a whole array diff concept, but here's a quick intro.
Let's say we are building a cat app. To keep things simple, let's have a Cat model object class like this:
@interface Cat : NSObject
@property (nonatomic, readonly) NSString *uid;
@property (nonatomic, readonly) NSString *name;
+ (instancetype)catWithUid:(NSString *)uid name:(NSString *)name;
@end
Somewhere in the app we'd have a list of cats, typically displayed via UITableView or UICollectionView. It's all straightforward until we have to animate an update. To pull this off, we need to know which items exactly were deleted, inserted, changed or moved. Here is how we can do this:
NSArray<Cat *> *catsBefore = @[ ... ];
NSArray<Cat *> *catsAfter = @[ ... ];
NNSectionsDiffCalculator *diffCalculator = [[NNSectionsDiffCalculator alloc] init];
diffCalculator.objectIdBlock = ^(Cat *cat) {
return cat.uid;
};
NNSectionsDiff *diff = [diffCalculator calculateDiffForSingleSectionObjectsBefore:catsBefore andAfter:catsAfter];
// The diff object now contains all needed info about mutated indexPaths.
// The easiest way to apply it to UITableView or UICollectionView is a bunch of category methods:
[self.tableView reloadWithSectionsDiff:diff];
That's pretty much it! The reloading routine is highly customizable and takes care of numerous UIKit quirks & bugs which may cause crashes.
Feel free to check out the demo project. A more detailed guide is hopefully coming soon :)
Related Skills
node-connect
339.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
