Skip to content

Instantly share code, notes, and snippets.

@casademora
Created April 4, 2014 18:41
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 casademora/8f70f44c3f9fcea3682f to your computer and use it in GitHub Desktop.
Save casademora/8f70f44c3f9fcea3682f to your computer and use it in GitHub Desktop.
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