objective-c中遍历对象中所有成员变量
1 Class cls = [self class]; 2 while (cls != [NSObject class]) 3 { 4 unsigned int numberOfIvars = 0; 5 Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);//获取cls 类成员变量列表 6 for(const Ivar* p = ivars; p < ivars+numberOfIvars; p++)//采用指针+1 来获取下一个变量 7 { 8 Ivar const ivar = *p;//取得这个变量 9 const char *type = ivar_getTypeEncoding(ivar); //取得这个变量的类型 10 NSString *key = [NSString stringWithUTF8String:ivar_getName(ivar)];//取得这个变量的名称 11 12 NSLog(@"++ Name=\"%@\" Type=%@ Value=%@",key,[NSString stringWithUTF8String:type],[self valueForKey:key]); 13 } 14 cls = class_getSuperclass(cls); 15 }
posted on 2012-09-04 17:07 cokecoffe 阅读(1245) 评论(0) 编辑 收藏 举报