Last active
August 29, 2015 14:09
-
-
Save yuuki1224/93b8aaba7d87c3eb4706 to your computer and use it in GitHub Desktop.
NSObject+Methods.h
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> | |
#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