获取一个属性的attributes
[objc] view plain copy @interface person : NSObject @property (nonatomic, strong) NSString *name; @end int main(){ unsigned int outCount = 0; objc_property_t *properties = class_copyPropertyList([person class], &outCount); for(int i = 0; i < outCount; i++){ unsigned int count = 0; objc_property_attribute_t *attributes = property_copyAttributesList(properties[i], &count); for(int j = 0; j < count; j++){ <span style="white-space:pre"> </span>NSLog(@"attribute.name = %s, attribute.value = %s", attributes[j].name, attributes[j].value); } } return 0; }
objc_property_attribute_t的value和name
常用attributenamevaluenonatomic"N"""strong/retain"&"""weak"W"""属性的类型type"T""@TypeName", eg"@\"NSString\""属性对应的实例变量Ivar"V""Ivar_name", eg "_name"readonly"R"""getter"G""GetterName", eg"isRight"setter"S""SetterName", eg"setName"assign/atomic默认即为assign和retain
动态添加属性
[objc] view plain copy @interface person : NSObjec{ NSString *_name; } int main(){ objc_property_attribute_t nonatomic = {"N", ""}; objc_property_attribute_t strong = {"&", ""}; objc_property_attribute_t type = {"T", "@\"NSString\""}; objc_property_attribute_t ivar = {"V", "_name"}; objc_property_attribute_t attributes[] = {nonatomic, strong, type, ivar}; BOOL result = class_addProperty([person class], "name", attributes, 4); 转载地址:http://blog.csdn.net/myzlk/article/details/50815381