我正在构建我的第一个 iPhone/Obj-c 应用程序,并且我有大量的数据保存子类,我将它们传递给引用函数。对于 cite 函数,这些对象是匿名的,我需要找到一种方法来访问每个传递对象的所有变量。
我一直在使用预构建的 NSArray 和选择器来执行此操作,但有超过 30 个条目(并且还在不断增长),手动执行此操作有点愚蠢。必须有一种方法来动态查找匿名对象的所有变量。
The obj-c 运行时 http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html运行时文档提到了这个问题,但据我所知,这在 iPhone 操作系统中不可用。如果是这样,那么我不了解实施情况,需要一些指导。类似的问题 https://stackoverflow.com/questions/754824/get-an-object-attributes-list-in-objective-c之前有人问过,但我再次认为他们谈论的是 OSX 而不是 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 上的运行时间相同。如果另一个问题符合您的要求,那么它应该有效。如果没有,请提交错误。
与此同时,给定一个Class
,您可以使用以下命令检索其所有选择器的列表class_copyMethodList() http://developer.apple.com/mac/library/documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html#//apple_ref/doc/uid/TP40001418-CH1g-SW4功能:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)