获取Objective-C类或实例的所有方法
在Objective-C中,我可以测试给定的类或实例是否响应某些选择器。但是如何查询类或实例的所有方法或类的属性(例如,所有方法或属性的列表)?这可以通过objc\u method\u list实现。为了枚举您的方法,您必须先注册所有方法 这个过程很简单:声明函数后,可以创建objc_方法的实例并注册函数名。然后将objc_方法添加到objc_方法列表中,最后将objc_方法列表传递给类_addMethods 以下是一个让您开始的链接:获取Objective-C类或实例的所有方法,objective-c,introspection,objective-c-runtime,Objective C,Introspection,Objective C Runtime,在Objective-C中,我可以测试给定的类或实例是否响应某些选择器。但是如何查询类或实例的所有方法或类的属性(例如,所有方法或属性的列表)?这可以通过objc\u method\u list实现。为了枚举您的方法,您必须先注册所有方法 这个过程很简单:声明函数后,可以创建objc_方法的实例并注册函数名。然后将objc_方法添加到objc_方法列表中,最后将objc_方法列表传递给类_addMethods 以下是一个让您开始的链接: 如果您想使用Objective C运行时方法,请参见此处:
如果您想使用Objective C运行时方法,请参见此处:您可以这样做,并且在 要获取类的所有实例或类方法,可以使用
class\u 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);
}
在元类上调用相同的方法将得到所有类方法
/**
* Calling the same on the metaclass gives you
* the class methods
*/
DumpObjcMethods(object_getClass(yourClass) /* Metaclass */);
除了Buzzy的答案外,出于调试目的,您还可以使用
-[NSObject\u 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)
......
:
在uu NSArrayM中:
类方法:
+(BOOL)automaticallyNotifiesObserversForKey:(id)arg1;(0x11503b510)
+(id)allocWithZone:(\u NSZone*)arg1;(0x11503b520)
+(id)uu new::(const id*)arg1;(0x114f0d700)
实例方法:
-(无效)移除对象;(0x114f669a0)
-(无效)解除锁定;(0x114f2a8f0)
-(b)最后确定;(0x11503b2c0)
-(id)copyWithZone:(\u NSZone*)arg1;(0x114f35500)
-(无符号长)计数;(0x114f0d920)
-(id)objectAtIndex:(无符号长)arg1;(0x114f2a730)
-(void)getObjects:(id*)arg1范围:(\u 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:(无符号长)arg1和ObjectAtIndex:(无符号长)arg2;(0x114f8bf80)
......
在NSMutableArray中:
类方法:
+(id)复印件不可收回阵列;(0x11ee20178)
+(id)非保留阵列;(0x11ee201e8)
+(id)非保留阵列;(0x120475026)
+(id)阵列容量:(无符号长)arg1;(0x114f74280)
......
导入路径很难找到,请使用:#导入谢谢,可能是输入错误,“DumpObjcMethods(object_getClass(yourClass)/*Metaclass/);”应该是:“DumpObjcMethods(object_getMetaClass(yourClass)/Metaclass*/);@Joey:我认为没有输入错误。没有名为object\u getMetaClass
的例程。类对象类是它的元类。这个链接现在重定向到导航页面,因为它变得更可怕了。下面Buzzy的答案是一个更好的答案,是目前为止最好的答案
<__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)
......