1、创建一个Person类
#import <Foundation/Foundation.h> @interface Person : NSObject { CGFloat height; } @property (nonatomic, strong) NSString *name; @property (nonatomic, assign) int age; @property (nonatomic, strong) NSNumber *ID; - (void)getPersonInfo; @end #import "Person.h" #import <objc/message.h> @implementation Person - (void)getPersonInfo { unsigned int outCount = 0; // 获取到所有的成员变量列表 Ivar *vars = class_copyIvarList([self class], &outCount); // 遍历所有的成员变量 for (int i = 0; i < outCount; i++) { // 取出第i个位置的成员变量 Ivar ivar = vars[i]; // 获取变量名 const char *propertyName = ivar_getName(ivar); // 获取变量编码类型 const char *propertyType = ivar_getTypeEncoding(ivar); printf("---%s--%s\n", propertyName, propertyType); } } @end
2、调用getPersonInfo可获得该类的相关信息
Runtime-遍历类的成员名称和类型,布布扣,bubuko.com
原文:http://blog.csdn.net/fucheng56/article/details/24034955