Last active
August 29, 2015 13:56
-
-
Save malcommac/8831903 to your computer and use it in GitHub Desktop.
DMMultiDelegatesProxy forwardInvocation code
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)forwardInvocation:(NSInvocation *)invocation { | |
// check if method can return something | |
BOOL methodReturnSomething = (![[NSString stringWithCString:invocation.methodSignature.methodReturnType encoding:NSUTF8StringEncoding] isEqualToString:@"v"]); | |
// send invocation to the main delegate and use it's return value | |
if ([self.mainDelegate respondsToSelector:invocation.selector]) | |
[invocation invokeWithTarget:self.mainDelegate]; | |
// make another fake invocation with the same method signature and send the same messages to the other delegates (ignoring return values) | |
NSInvocation *targetInvocation = invocation; | |
if (methodReturnSomething) { | |
targetInvocation = [NSInvocation invocationWithMethodSignature:invocation.methodSignature]; | |
[targetInvocation setSelector:invocation.selector]; | |
} | |
for (NSValue *nonRetainedValue in nonRetainedDelegates) { | |
id delegateObj = nonRetainedValue.nonretainedObjectValue; | |
if ([delegateObj respondsToSelector:invocation.selector]) | |
[targetInvocation invokeWithTarget:delegateObj]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment