Last active
December 21, 2015 08:38
-
-
Save shaps80/6279008 to your computer and use it in GitHub Desktop.
Determine if a subclass implements a specific selector.
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
if ([MyView implementsSelector:@selector(someMethod:)]) | |
NSLog(@"Implemented"); |
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 (SPXAdditions) | |
+ (BOOL)implementsSelector:(SEL)selector; | |
@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+SPXAdditions.h" | |
#import <objc/runtime.h> | |
@implementation NSObject (SPXAdditions) | |
+ (BOOL)implementsSelector:(SEL)selector | |
{ | |
int unsigned numMethods; | |
const char *className = [NSStringFromClass([self class]) UTF8String]; | |
Method *methods = class_copyMethodList(objc_getClass(className), &numMethods); | |
for (int i = 0; i < numMethods; i++) | |
{ | |
if (selector == method_getName(methods[i])) | |
return YES; | |
} | |
return NO; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment