Skip to content

Instantly share code, notes, and snippets.

@yuuki1224
Last active August 29, 2015 14:09
Show Gist options
  • Save yuuki1224/93b8aaba7d87c3eb4706 to your computer and use it in GitHub Desktop.
Save yuuki1224/93b8aaba7d87c3eb4706 to your computer and use it in GitHub Desktop.
NSObject+Methods.h
#import <Foundation/Foundation.h>
#import <objc/objc-runtime.h>
@interface NSObject (Methods)
- (NSArray *)methods;
- (NSArray *)methodsUntilRoot;
// TODO: classメソッド関連
// Method class_getClassMethod(Class cls, SEL name)
@end
@implementation NSObject (Methods)
- (NSArray *)methods
{
return [self methodsWithClass:self.class];
}
- (NSArray *)methodsUntilRoot
{
NSMutableArray *methodsArray = [NSMutableArray new];
Class cls = self.class;
while (cls != nil) {
[methodsArray addObject:[self methodsWithClass:cls]];
cls = class_getSuperclass(cls);
}
return [methodsArray copy];
}
- (NSArray *)methodsWithClass:(Class)cls
{
NSMutableArray *methodsArray = [NSMutableArray new];
unsigned int numMethods;
Method *methods = class_copyMethodList(cls, &numMethods);
for (unsigned int i = 0; i < numMethods; i++) {
Method method = methods[i];
SEL selector = method_getName(method);
NSString *methodName = NSStringFromSelector(selector);
[methodsArray addObject:methodName];
}
NSLog(@"%@", methodsArray);
return [methodsArray copy];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment