Skip to content

Instantly share code, notes, and snippets.

View jrturton's full-sized avatar

Richard Turton jrturton

View GitHub Profile
@alanjrogers
alanjrogers / KVO.m
Created July 4, 2012 01:15
KVO best practices
static NSString *MYObservationContext = @"MYObservationContext";
[object addObserver:self forKeyPath:@"key.path" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:(void*)&MYObservationContext];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &MYObservationContext) {
id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
id newValue = [change objectForKey:NSKeyValueChangeNewKey];
// Do something
}