Last active
August 29, 2015 14:07
-
-
Save yurikoles/0f882bf014639f71f6d9 to your computer and use it in GitHub Desktop.
Useful category for delegates. WARNING: it's not finished yet.
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
#import <Foundation/Foundation.h> | |
@interface NSObject (YKSafeSelector) | |
- (void)safeSelector:(SEL)selector, ... NS_REQUIRES_NIL_TERMINATION; | |
@end |
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
#import "NSObject+YKSafeSelector.h" | |
@implementation NSObject (YKSafeSelector) | |
- (void)safeSelector:(SEL)selector, ... | |
{ | |
if (![self respondsToSelector:selector]) | |
return; | |
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:selector]]; | |
[inv setSelector:selector]; | |
[inv setTarget:self]; | |
va_list argumentList; | |
if (selector) { | |
va_start(argumentList, (SEL)selector); | |
NSInteger idx = 2; | |
id eachObject = nil; | |
while ((eachObject = va_arg(argumentList, id))){ | |
[inv setArgument:(__bridge void *)(eachObject) atIndex:idx]; | |
idx++; | |
} | |
va_end(argumentList); | |
} | |
[inv invoke]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment