Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Dynamic KVO Handler method
- (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
You can’t perform that action at this time.