Skip to content

Instantly share code, notes, and snippets.

@Tricertops
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tricertops/103465a5bf07ae005acd to your computer and use it in GitHub Desktop.
Save Tricertops/103465a5bf07ae005acd to your computer and use it in GitHub Desktop.
@interface List : NSObject
@property (copy) NSArray *items;
@property (readonly) NSMutableArray *mutableItems;
@end
@implementation List
- (NSMutableArray *)mutableItems {
// KVO trick: returns a proxy array that uses "items" key to get/set the value.
return [self mutableArrayValueForKey:@"items"];
}
@end
// Usage:
[list.mutableItems addObject:item];
// Equivalent of:
NSMutableArray *mutableItems = [list.items mutableCopy];
[mutableItems addObject:item];
list.items = mutableItems;
// Pro: Easiest way to trigger KVO events for collection mutations.
// Con: Too much copying when doing a lot of mutating operations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment