Skip to content

Instantly share code, notes, and snippets.

@ddeville
Created October 26, 2013 19:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddeville/7173457 to your computer and use it in GitHub Desktop.
Save ddeville/7173457 to your computer and use it in GitHub Desktop.
NSArrayController and +keyPathsForValuesAffectingValueForKey:
//
// 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