Skip to content

Instantly share code, notes, and snippets.

@collindonnell
Last active August 29, 2015 14:03
Show Gist options
  • Save collindonnell/99f392901fb425d6b3d8 to your computer and use it in GitHub Desktop.
Save collindonnell/99f392901fb425d6b3d8 to your computer and use it in GitHub Desktop.
Notify a target with the specified arguments only if it responds to a selector.
- (void)safelyMessageTarget:(id)target withSelector:(SEL)selector arguments:(NSArray *)arguments {
if ([target respondsToSelector:selector]) {
NSMethodSignature *methodSignature = [NSMethodSignature methodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[arguments enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[invocation setArgument:(__bridge void *)(obj) atIndex:idx];
}];
[invocation invokeWithTarget:target];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment