Last active
August 29, 2015 14:07
-
-
Save Tricertops/103465a5bf07ae005acd to your computer and use it in GitHub Desktop.
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
@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 |
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
// 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