2010-01-19 2 views
32

Objective-C에서는 주어진 클래스 나 인스턴스가 특정 선택자에 응답하는지 테스트 할 수 있습니다. 그러나 클래스 나 인스턴스에 대해 클래스의 모든 메서드 나 속성 (예 : 모든 메서드 또는 속성의 목록)을 쿼리하는 방법은 무엇입니까?objective-c 클래스 또는 인스턴스의 모든 메소드를 얻으시겠습니까?

문서화 된 것이 든 아니든, 예를 들면 다음과 같이 가능해야합니다. WebView은 스크립트에서 볼 수 있어야하는 경우 모든 메소드 및 속성에 대해 플러그인 스크립트 가능 객체를 쿼리 할 수 ​​있습니다.

답변

10

당신은 목표 C 런타임 방법을 사용하는 것이 좋습니다 여기를 참조 : 여기

당신이 시작하는 링크입니다 .. 그런 다음 objc_method_list에 objc_method를 추가하고 마지막으로 class_addMethods에 objc_method_list을 통과 : https://developer.apple.com/reference/objectivec/objective_c_runtime

+1

이 링크는 지금 리디렉션 문서가 입수했습니다 – user3125367

+0

사과 탐색 페이지 :

@interface NSObject (Private) - (NSString*)_methodDescription; @end // Somewhere in the code: NSLog(@"%@", [objectToInspect performSelector:@selector(_methodDescription)]); 

출력의 뜻은 다음과 같습니다 더 끔찍한. 아래 Buzzy의 대답은 훨씬 더 좋은 대답입니다. – Paul

4

objc_method_list를 통해 가능합니다. 귀하의 방법을 열거하기 위해서는 모든 방법을 사전에 등록해야합니다.

프로세스가 간단합니다. 함수를 선언 한 후에는 objc_method의 인스턴스를 만들고 함수 이름을 등록 할 수 있습니다. http://theocacao.com/document.page/327

33

당신은이 작업을 수행 할 수 있습니다 매우 잘 클래스의 모든 인스턴스 또는 클래스 메소드를 가져올 수 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); 
} 

이 방법으로 두 개의 통화를해야합니다. 클래스 메소드의 인스턴스 방법과 또 다른 한 가지 : 메타 클래스에서 동일한 호출

/** 
* 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 */); 
+1

가져 오기 경로를 찾기가 어렵습니다. #import prewett

+0

감사합니다. 대답을 편집하여 – Buzzy

+0

을 입력하십시오. 감사합니다. 오타가있을 수 있습니다. "DumpObjcMethods (object_getClass (yourClass)/* Metaclass * /);" 은 다음과 같아야합니다. "DumpObjcMethods (object_getMetaClass (yourClass)/* Metaclass * /);" – Joey

17

을 줄 것이다. 디버깅 목적으로 -[NSObject _methodDescription] 개인 메소드를 사용할 수 있습니다.

는 lldb에서 다음 중 하나를

(lldb) po [[UIApplication sharedApplication] _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) 
     ...... 
+1

지금까지 최선의 답변 – mafonya