Skip to content

Instantly share code, notes, and snippets.

@malcommac
Last active August 29, 2015 13:56
Show Gist options
  • Save malcommac/8831903 to your computer and use it in GitHub Desktop.
Save malcommac/8831903 to your computer and use it in GitHub Desktop.
DMMultiDelegatesProxy forwardInvocation code
- (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