iPhone OS:从匿名对象获取方法和变量列表 [英] iPhone OS: Get a list of methods and variables from anonymous object

查看:103
本文介绍了iPhone OS:从匿名对象获取方法和变量列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建我的第一个iPhone / Obj-c应用程序,我有大量的数据保存子类,我将其传递给一个引用函数。对于引用函数,这些对象是匿名的,我需要找到一种方法来访问每个传递对象的所有变量。
我一直在使用预先构建的NSArray和选择器来执行此操作,但是有超过30个条目(并且正在增长),手动执行它有点愚蠢。必须有一种动态查找匿名对象的所有变量的方法。

I am building my first iPhone/Obj-c app and I have a large amount of data-holding subclasses that I am passing into a cite function. To the cite function these objects are anonymous and I need to find a way to access all the variables of each passed object. I have been using a pre-built NSArray and Selectors to do this but with more than 30 entries (and growing) it is kind of silly to do manually. There has to be a way to dynamically look up all the variables of an anonymous object.

obj-c运行时运行时文档提到了这个问题,但从我所知道的这个不可用iPhone OS。如果是,那么我不理解实施,需要一些指导。 之前曾问过类似问题,但我认为他们是谈论OSX而不是iPhone。

The obj-c runtime run-time docs mention this problem but from what I can tell this is not available in iPhone OS. If it is then I don't understand the implementation and need some guidance. A similar question was asked before but again I think they were talking about OSX and not iPhone.

有什么想法吗?

-(NSString*)cite:(id)source {
 NSString *sourceClass = NSStringFromClass([source class]);

 // Runs through all the variables in the manually built methodList
 for(id method in methodList) {
  SEL x = NSSelectorFromString(method);
  // further implementation

 // Should be something like
 NSArray *methodList = [[NSArray alloc] initWithObjects:[source getVariableList]]
 for(id method in methodList) {
  SEL x = NSSelectorFromString(method);
  // Further implementation
 }


推荐答案

Mac上的运行时与iPhone上的运行时相同。如果另一个问题符合您的要求,那么它应该有效。如果没有,请提交错误。

The runtime is the same on the Mac as it is on the iPhone. If the other question does what you're looking for, then it should work. If it doesn't, file a bug.

在此期间,给定 Class ,您可以检索使用 class_copyMethodList() 功能:

In the meantime, given a Class, you can retrieve a list of all of its selectors using the class_copyMethodList() function:

unsigned int numMethods = 0;
Method * methods = class_copyMethodList(sourceClass, &numMethods);
NSMutableArray * selectors = [NSMutableArray array];
for (int i = 0; i < numMethods; ++i) {
  SEL selector = method_getName(methods[i]);
  [selectors addObject:NSStringFromSelector(selector)];
}
free(methods);

这篇关于iPhone OS:从匿名对象获取方法和变量列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆