Created
October 26, 2013 19:19
-
-
Save ddeville/7173457 to your computer and use it in GitHub Desktop.
NSArrayController and +keyPathsForValuesAffectingValueForKey:
This file contains hidden or 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
// | |
// main.m | |
// KVO | |
// | |
// Created by Damien DeVille on 10/26/13. | |
// Copyright (c) 2013 Damien DeVille. All rights reserved. | |
// | |
#import <Cocoa/Cocoa.h> | |
@interface Shop : NSObject | |
@property (readonly, strong, nonatomic) NSArray *products; | |
- (void)addProduct:(NSString *)product; | |
- (void)removeProduct:(NSString *)product; | |
@end | |
@interface Shop (/* Private */) | |
@property (strong, nonatomic) NSArrayController *productsController; | |
@property (readwrite, strong, nonatomic) NSArray *products; | |
@end | |
@implementation Shop | |
static NSString * _ShopProductsControllerArrangedObjectsObservationContext = @"_ShopProductsControllerArrangedObjectsObservationContext"; | |
- (id)init | |
{ | |
self = [super init]; | |
if (self == nil) { | |
return nil; | |
} | |
NSMutableArray *products = [NSMutableArray arrayWithObjects:@"Cheetah", @"Puma", @"Jaguar", @"Panther", @"Tiger", @"Leopard", @"Snow Leopard", @"Lion", @"Mountain Lion", nil]; | |
_productsController = [[NSArrayController alloc] initWithContent:products]; | |
[_productsController addObserver:self forKeyPath:@"arrangedObjects" options:(NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:&_ShopProductsControllerArrangedObjectsObservationContext]; | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[_productsController removeObserver:self forKeyPath:@"arrangedObjects" context:&_ShopProductsControllerArrangedObjectsObservationContext]; | |
} | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context | |
{ | |
if (context == &_ShopProductsControllerArrangedObjectsObservationContext) { | |
NSArray *products = [NSArray arrayWithArray:[[self productsController] arrangedObjects]]; | |
[self setProducts:products]; | |
} | |
else { | |
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | |
} | |
} | |
- (void)addProduct:(NSString *)product | |
{ | |
[[self productsController] addObject:product]; | |
} | |
- (void)removeProduct:(NSString *)product | |
{ | |
[[self productsController] removeObject:product]; | |
} | |
@end | |
#pragma mark - | |
@interface Observer : NSObject | |
@end | |
@implementation Observer | |
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context | |
{ | |
// nop | |
} | |
@end | |
#pragma mark - | |
int main(int argc, const char **argv) | |
{ | |
Shop *shop = [[Shop alloc] init]; | |
Observer *observer = [[Observer alloc] init]; | |
[shop addObserver:observer forKeyPath:@"products" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:NULL]; | |
[shop addProduct:@"Mavericks"]; | |
[shop removeObserver:observer forKeyPath:@"products" context:NULL]; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment