/gist:8f70f44c3f9fcea3682f Secret
Created
April 4, 2014 18:41
Dynamic KVO Handler method
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
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context; | |
{ | |
if (context == kTBBookStoreCollectionViewCellObservingContext) | |
{ | |
if (object == self.bookStoreItem) | |
{ | |
NSString *observationSelectorName = [NSString stringWithFormat:@"%@DidChangeFromValue:toValue:", keyPath]; | |
SEL observationSelector = NSSelectorFromString(observationSelectorName); | |
if ([self respondsToSelector:observationSelector]) | |
{ | |
NSMethodSignature *methodSignature = [self methodSignatureForSelector:observationSelector]; | |
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; | |
invocation.selector = observationSelector; | |
id oldValue = [change valueForKey:NSKeyValueChangeOldKey]; | |
id newValue = [change valueForKey:NSKeyValueChangeNewKey]; | |
oldValue = [oldValue isEqual:[NSNull null]] ? nil : oldValue; | |
newValue = [newValue isEqual:[NSNull null]] ? nil : newValue; | |
[invocation setArgument:&oldValue atIndex:2]; | |
[invocation setArgument:&newValue atIndex:3]; | |
[invocation invokeWithTarget:self]; | |
} | |
} | |
} | |
else | |
{ | |
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment