Skip to content

Instantly share code, notes, and snippets.

@shaps80
Last active December 21, 2015 08:38
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaps80/6279008 to your computer and use it in GitHub Desktop.
Save shaps80/6279008 to your computer and use it in GitHub Desktop.
Determine if a subclass implements a specific selector.
if ([MyView implementsSelector:@selector(someMethod:)])
NSLog(@"Implemented");
#import <Foundation/Foundation.h>
@interface NSObject (SPXAdditions)
+ (BOOL)implementsSelector:(SEL)selector;
@end
#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