Objective-C对象的列表选择器 [英] List selectors for Objective-C object
本文介绍了Objective-C对象的列表选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个对象,我想列出它响应的所有选择器.感觉这完全有可能,但是我在查找API时遇到了麻烦.
I have an object, and I want to list all the selectors to which it responds. It feels like this should be perfectly possible, but I'm having trouble finding the APIs.
推荐答案
这是基于运行时C函数的解决方案:
This is a solution based on the runtime C functions:
class_copyMethodList返回可从对象获取的给定Class对象的类方法列表.
class_copyMethodList returns a list of class methods given a Class object obtainable from an object.
#import <objc/runtime.h>
[..]
SomeClass * t = [[SomeClass alloc] init];
int i=0;
unsigned int mc = 0;
Method * mlist = class_copyMethodList(object_getClass(t), &mc);
NSLog(@"%d methods", mc);
for(i=0;i<mc;i++)
NSLog(@"Method no #%d: %s", i, sel_getName(method_getName(mlist[i])));
/* note mlist needs to be freed */
这篇关于Objective-C对象的列表选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文