Skip to content

Commit

Permalink
Improve evaluation order
Browse files Browse the repository at this point in the history
Save accessing a associated object until we've verified that the method exists.
  • Loading branch information
ikesyo committed Nov 11, 2013
1 parent 0bc09f6 commit dcc5107
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions ReactiveCocoaFramework/ReactiveCocoa/NSObject+RACSelectorSignal.m
Expand Up @@ -98,20 +98,20 @@ static void RACSwizzleRespondsToSelector(Class class) {
// Set up a new version of -respondsToSelector: that returns YES for methods
// added by -rac_signalForSelector:.
//
// If the selector has a method defined on the receiver's actual class, and
// if that method's implementation is _objc_msgForward, and the instance has
// a signal for the selector, then return YES.
// Otherwise, return logical AND of the original -respondsToSelector:'s
// return value and whether the instance has a signal for the selector.
// If the selector has a method defined on the receiver's actual class and
// if that method's implementation is _objc_msgForward, or, if the original
// -respondsToSelector: returns YES, then returns whether the instance has a
// signal for the selector. Otherwise, returns NO.
id newRespondsToSelector = ^ BOOL (id self, SEL selector) {
SEL aliasSelector = RACAliasForSelector(selector);
BOOL hasSignalForSelector = objc_getAssociatedObject(self, aliasSelector) != nil;

Method method = rac_getImmediateInstanceMethod(object_getClass(self), selector);

if (method != NULL && method_getImplementation(method) == _objc_msgForward && hasSignalForSelector) return YES;

return originalRespondsToSelector(self, respondsToSelectorSEL, selector) && hasSignalForSelector;
BOOL hasMessageForwardingImplementation = (method != NULL && method_getImplementation(method) == _objc_msgForward);

if (hasMessageForwardingImplementation || originalRespondsToSelector(self, respondsToSelectorSEL, selector)) {
SEL aliasSelector = RACAliasForSelector(selector);
return objc_getAssociatedObject(self, aliasSelector) != nil;
}

return NO;
};

class_replaceMethod(class, @selector(respondsToSelector:), imp_implementationWithBlock(newRespondsToSelector), method_getTypeEncoding(respondsToSelectorMethod));
Expand Down

0 comments on commit dcc5107

Please sign in to comment.