在iOS程序的运行时我们可以获取类中的 属性、方法、变量、协议等等,来看一下如何去获取这些:
//获取属性列表
objc_property_t *propertyList = class_copyPropertyList([NSObject class], &count);
for (unsigned int i=0; i%@", [NSString stringWithUTF8String:propertyName]);
}
free(propertyList);
//获取方法列表
Method *methodList = class_copyMethodList([NSObject class], &count);
for (unsigned int i; i%@", NSStringFromSelector(method_getName(method)));
}
//运行时环境没有引用计数,所以没有等价的retain或者release方法,如果从带有copy的函数得到一个值,就应该free,如果用了不带copy单词的函数,千万不要调用free。
free(methodList);
//获取成员变量列表
Ivar *ivarList = class_copyIvarList([NSObject class], &count);
for (unsigned int i; i%@", [NSString stringWithUTF8String:ivarName]);
}
free(ivarList);
//获取协议列表
__unsafe_unretained Protocol **protocolList = class_copyProtocolList([NSObject class], &count);
for (unsigned int i; i%@", [NSString stringWithUTF8String:protocolName]);
}
free(protocolList);