42

Objective-C では、特定のクラスまたはインスタンスが特定のセレクターに応答するかどうかをテストできます。しかし、クラスまたはインスタンスのすべてのメソッドまたはクラスのプロパティ (たとえば、すべてのメソッドまたはプロパティのリスト) を照会するにはどうすればよいでしょうか?

4

4 に答える 4

56

Buzzy の回答に加えて、デバッグ目的で-[NSObject _methodDescription]プライベート メソッドを使用できます。

lldb のいずれか:

(lldb) po [[UIApplication sharedApplication] _methodDescription]

またはコードで:

@interface NSObject (Private)
- (NSString*)_methodDescription;
@end

// Somewhere in the code:
NSLog(@"%@", [objectToInspect performSelector:@selector(_methodDescription)]);

出力は次のようになります。

<__NSArrayM: 0x7f9 ddc4359a0>:
in __NSArrayM:
    Class Methods:
        + (BOOL) automaticallyNotifiesObserversForKey:(id)arg1; (0x11503b510)
        + (id) allocWithZone:(_NSZone*)arg1; (0x11503b520)
        + (id) __new:::::(const id*)arg1; (0x114f0d700)
    Instance Methods:
        - (void) removeLastObject; (0x114f669a0)
        - (void) dealloc; (0x114f2a8f0)
        - (void) finalize; (0x11503b2c0)
        - (id) copyWithZone:(_NSZone*)arg1; (0x114f35500)
        - (unsigned long) count; (0x114f0d920)
        - (id) objectAtIndex:(unsigned long)arg1; (0x114f2a730)
        - (void) getObjects:(id*)arg1 range:(_NSRange)arg2; (0x114f35650)
        - (void) addObject:(id)arg1; (0x114f0d8e0)
        - (void) setObject:(id)arg1 atIndex:(unsigned long)arg2; (0x114f99680)
        - (void) insertObject:(id)arg1 atIndex:(unsigned long)arg2; (0x114f0d940)
        - (void) exchangeObjectAtIndex:(unsigned long)arg1 withObjectAtIndex:(unsigned long)arg2; (0x114f8bf80)
        ......
in NSMutableArray:
    Class Methods:
        + (id) copyNonRetainingArray; (0x11ee20178)
        + (id) nonRetainingArray; (0x11ee201e8)
        + (id) nonRetainingArray; (0x120475026)
        + (id) arrayWithCapacity:(unsigned long)arg1; (0x114f74280)
        ......
于 2016-07-21T10:38:20.440 に答える
47

これを行うことができ、https://developer.apple.com/library/mac/documentation/cocoa/Reference/ObjCRuntimeRef/index.htmlで非常によく文書化されています。

クラスのすべてのインスタンスまたはクラス メソッドを取得するにはclass_copyMethodList、結果を使用して反復処理することができます。例:

 #import <objc/runtime.h>

/**
 *  Gets a list of all methods on a class (or metaclass)
 *  and dumps some properties of each
 *
 *  @param clz the class or metaclass to investigate
 */
void DumpObjcMethods(Class clz) {

    unsigned int methodCount = 0;
    Method *methods = class_copyMethodList(clz, &methodCount);

    printf("Found %d methods on '%s'\n", methodCount, class_getName(clz));

    for (unsigned int i = 0; i < methodCount; i++) {
        Method method = methods[i];

        printf("\t'%s' has method named '%s' of encoding '%s'\n",
               class_getName(clz),
               sel_getName(method_getName(method)),
               method_getTypeEncoding(method));

        /**
         *  Or do whatever you need here...
         */
    }

    free(methods);
}

このメソッドを 2 回個別に呼び出す必要があります。1 つはインスタンス メソッド用で、もう 1 つはクラス メソッド用です。

/**
 *  This will dump all the instance methods
 */
DumpObjcMethods(yourClass);

メタクラスで同じものを呼び出すと、すべてのクラス メソッドが得られます

/**
 *  Calling the same on the metaclass gives you
 *  the class methods
 */
DumpObjcMethods(object_getClass(yourClass) /* Metaclass */);
于 2014-11-22T02:11:01.923 に答える
9

Objective C ランタイム メソッドを使用する場合は、こちらを参照してください: https://developer.apple.com/reference/objectivec/objective_c_runtime

于 2010-01-19T16:01:04.037 に答える
4

これはobjc_method_listを介して可能です。メソッドを列挙するには、事前にすべてのメソッドを登録する必要があります。

プロセスは簡単です。関数を宣言した後、objc_methodのインスタンスを作成し、関数名を登録できます。次に、objc_methodをobjc_method_listに追加し、最後にobjc_method_listをclass_addMethodsに渡します。

開始するためのリンクは次のとおりです:http: //theocacao.com/document.page/327

于 2010-01-19T16:00:52.060 に答える