Chr*_*son 3 iphone objective-c
我正在构建我的第一个iPhone/Obj-c应用程序,我有大量的数据保存子类,我将其传递给一个引用函数.对于引用函数,这些对象是匿名的,我需要找到一种方法来访问每个传递对象的所有变量.我一直在使用预先构建的NSArray和选择器来执行此操作,但是有超过30个条目(并且正在增长),手动执行它是一种愚蠢的行为.必须有一种方法来动态查找匿名对象的所有变量.
该OBJ-C运行时运行文档提到这个问题,但据我可以告诉这是不是可以在iPhone OS.如果是,那么我不理解实施,需要一些指导.之前曾问过一个类似的问题,但我认为他们再次讨论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
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ong 11
Mac上的运行时与iPhone上的运行时相同.如果另一个问题符合您的要求,那么它应该有效.如果没有,请提交错误.
在此期间,给定a Class,您可以使用以下class_copyMethodList()函数检索所有选择器的列表:
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);
Run Code Online (Sandbox Code Playgroud)
通过Objective-C运行时函数可以做到这一点,但它可能不是正确的方法.由于您正在创建传递给cite方法的对象,因此只需让它们各自实现一个可以用来提取所需信息的协议.
类似于键值编码协议的东西可能会做你想要的:http: //developer.apple.com/iphone/library/documentation/cocoa/Conceptual/KeyValueCoding/KeyValueCoding.html
| 归档时间: |
|
| 查看次数: |
1627 次 |
| 最近记录: |