Subscribed unsubscribe Subscribe Subscribe

指定したクラスの、実装されているメソッドを列挙する

Objective-C
#import <Foundation/Foundation.h>
#import <objc/runtime.h>

int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    unsigned int i, count;
    Method *methods;
    Class target_class = [NSIndexPath class];    // ターゲットのクラス

    // インスタンスメソッド
    NSLog(@"Instance Methods:");
    methods = class_copyMethodList(target_class, &count);
    for (i = 0; i < count; i++) {
        NSLog(@"%3d: %@",
              i + 1,
              NSStringFromSelector(method_getName(methods[i])));
    }
    
    // クラスメソッド
    NSLog(@"Class Methods:");
    methods = class_copyMethodList(object_getClass(target_class), &count);
    for (i = 0; i < count; i++) {
        NSLog(@"%3d: %@",
              i + 1,
              NSStringFromSelector(method_getName(methods[i])));
    }

    [pool release];

    return 0;
}


たとえばNSIndexPathをターゲットとして実行すると

$ gcc -framework Foundation hoge.m
$ ./a.out 
2009-04-06 14:33:57.051 a.out[8739:10b] Instance Methods:
2009-04-06 14:33:57.053 a.out[8739:10b]   1: compare:
2009-04-06 14:33:57.053 a.out[8739:10b]   2: description
2009-04-06 14:33:57.054 a.out[8739:10b]   3: getIndexes:
2009-04-06 14:33:57.054 a.out[8739:10b]   4: indexAtPosition:
2009-04-06 14:33:57.054 a.out[8739:10b]   5: length
2009-04-06 14:33:57.055 a.out[8739:10b]   6: indexPathByAddingIndex:
2009-04-06 14:33:57.055 a.out[8739:10b]   7: indexPathByRemovingLastIndex
2009-04-06 14:33:57.055 a.out[8739:10b]   8: initWithCoder:
2009-04-06 14:33:57.056 a.out[8739:10b]   9: encodeWithCoder:
2009-04-06 14:33:57.056 a.out[8739:10b]  10: copyWithZone:
2009-04-06 14:33:57.056 a.out[8739:10b]  11: dealloc
2009-04-06 14:33:57.057 a.out[8739:10b]  12: finalize
2009-04-06 14:33:57.057 a.out[8739:10b]  13: initWithIndexes:length:
2009-04-06 14:33:57.058 a.out[8739:10b]  14: initWithIndex:
2009-04-06 14:33:57.058 a.out[8739:10b]  15: init
2009-04-06 14:33:57.058 a.out[8739:10b] Class Methods:
2009-04-06 14:33:57.059 a.out[8739:10b]   1: indexPathWithIndexes:length:
2009-04-06 14:33:57.070 a.out[8739:10b]   2: indexPathWithIndex:
2009-04-06 14:33:57.071 a.out[8739:10b]   3: _uniquer

こんなカンジで取れた。これでいいのかな。