方法声明:
objc_property_t class_getProperty ( Class cls, const char *name );
描述:返回给定类的指定名称的属性的描述信息;返回的objc_property_t类型的内容通过property_getName,property_getAttributes,property_copyAttributeValue,property_copyAttributeList方法查看
方法声明:
objc_property_t * class_copyPropertyList ( Class cls, unsigned int *outCount );
描述:
cls |
需要操作的类 |
outCount |
输出指针,指明返回objc_property_t类型数组的大小 |
方法声明:
BOOL class_addProperty ( Class cls, const char *name, const objc_property_attribute_t*attributes, unsigned int attributeCount );
描述:给类添加一个属性
cls |
要修改的类 |
name |
属性的名称 |
attributes |
属性相关特性的数组 |
attributeCount |
|
方法声明:
void class_replaceProperty ( Class cls, const char *name, const objc_property_attribute_t*attributes, unsigned int attributeCount );
描述:替换类的一个属性
cls |
要修改的类 |
name |
属性的名称 |
attributes |
属性相关特性的数组 |
attributeCount |
数组 |
方法声明:
const char * property_getName ( objc_property_t property );
描述:返回属性描述信息中的属性名称的c字符串
方法声明:
const char * property_getAttributes ( objc_property_t property );
描述:返回属性描述信息中的属性特性的c字符串
方法声明:
char * property_copyAttributeValue ( objc_property_t property, const char *attributeName );
描述:返回属性描述信息中的属性特性名称为attributeName的c字符串,返回的字符串使用完以后需要自己通过free方法释放掉;
方法声明:
objc_property_attribute_t * property_copyAttributeList ( objc_property_t property, unsignedint *outCount );
描述:获取属性的特性的数组,数组的个数会返回给输出指针outCount,返回的数组使用完以后需要自己通过free方法释放掉;
举例:
@interface Book : BaseModel { @private NSString* _privateName; } @property (strong, nonatomic) NSString *author; @property (assign, nonatomic) NSUInteger pages; @property (strong, nonatomic) Pen *pen; + (void)ClassMethod; - (void)InstanceMethod; @end
Class cls = [Book class]; unsigned int ivarsCnt = 0; objc_property_t property = class_getProperty( cls, "author" ); NSLog(@"property:%s,%s", property_getName(property),property_getAttributes(property)); // 获取author属性的特征信息列表 objc_property_attribute_t *attributes = property_copyAttributeList(property, &ivarsCnt); for (objc_property_attribute_t *p = attributes; p < attributes + ivarsCnt; p++) { objc_property_attribute_t attribute = *p; NSLog(@"name: %s value: %s", attribute.name,attribute.value); char* value = property_copyAttributeValue ( property, attribute.name ); NSLog(@"value: %s ", value); free(value); } free(attributes);
打印信息:
2015-09-21 10:09:20.482 AppTest[4029:56317] property:author,T@"NSString",&,N,V_author
2015-09-21 10:09:20.482 AppTest[4029:56317] name: T value: @"NSString"
2015-09-21 10:09:20.483 AppTest[4029:56317] value: @"NSString"
2015-09-21 10:09:20.483 AppTest[4029:56317] name: & value:
2015-09-21 10:09:20.483 AppTest[4029:56317] value:
2015-09-21 10:09:20.483 AppTest[4029:56317] name: N value:
2015-09-21 10:09:20.483 AppTest[4029:56317] value:
2015-09-21 10:09:20.483 AppTest[4029:56317] name: V value: _author
2015-09-21 10:09:20.483 AppTest[4029:56317] value: _author
说明:属性的特性是固定格式,以逗号隔开,通常是T@"类型"开头,V_属性名称结尾的格式,中间的参考表:
举例:
Class cls = [Book class]; objc_property_attribute_t attr0 = { "T", "@\"NSArray\"" }; objc_property_attribute_t attr1 = { "&", "" }; objc_property_attribute_t attr2 = { "N", "" }; objc_property_attribute_t attr3 = { "V", "newProperty" }; objc_property_attribute_t attrs[] = { attr0, attr1, attr2,attr3 }; // 添加一个名字为newProperty的NSArray类型的属性 BOOL ret = class_addProperty ( cls, "newProperty", attrs, 4 ); if (ret) { // 获取所以属性的列表 unsigned int count; objc_property_t *properties = class_copyPropertyList(cls, &count); for(int i = 0; i < count; i++) { objc_property_t property = properties[i]; NSLog(@"name:%s",property_getName(property)); NSLog(@"attributes:%s",property_getAttributes(property)); } free(properties); }打印信息:
2015-09-21 10:21:42.141 AppTest[4449:62700] name:newProperty
2015-09-21 10:21:42.142 AppTest[4449:62700] attributes:T@"NSArray",&,N,VnewProperty
2015-09-21 10:21:42.142 AppTest[4449:62700] name:author
2015-09-21 10:21:42.142 AppTest[4449:62700] attributes:T@"NSString",&,N,V_author
2015-09-21 10:21:42.142 AppTest[4449:62700] name:pages
2015-09-21 10:21:42.142 AppTest[4449:62700] attributes:TQ,N,V_pages
2015-09-21 10:21:42.142 AppTest[4449:62700] name:pen
2015-09-21 10:21:42.143 AppTest[4449:62700] attributes:T@"Pen",&,N,V_pen
说明:
从打印信息看出我们的新属性已经添加成功;
Class cls = [Book class]; objc_property_attribute_t attr0_ = { "T", "@\"NSArray\"" }; objc_property_attribute_t attr1_ = { "&", "" }; objc_property_attribute_t attr2_ = { "N", "" }; objc_property_attribute_t attr3_ = { "V", "author" }; objc_property_attribute_t attrs_[] = { attr0_, attr1_, attr2_,attr3_ }; class_replaceProperty ( cls, "author", attrs_, 4 ); // 替换现有的author属性的类型为数组类型 unsigned int count; objc_property_t *properties = class_copyPropertyList(cls, &count); for(int i = 0; i < count; i++) { objc_property_t property = properties[i]; NSLog(@"name:%s",property_getName(property)); NSLog(@"attributes:%s",property_getAttributes(property)); } free(properties);
2015-09-21 10:24:13.236 AppTest[4555:64798] name:author
2015-09-21 10:24:13.237 AppTest[4555:64798] attributes:T@"NSArray",&,N,Vauthor
2015-09-21 10:24:13.237 AppTest[4555:64798] name:pages
2015-09-21 10:24:13.237 AppTest[4555:64798] attributes:TQ,N,V_pages
2015-09-21 10:24:13.237 AppTest[4555:64798] name:pen
2015-09-21 10:24:13.237 AppTest[4555:64798] attributes:T@"Pen",&,N,V_pen
说明:
从打印信息看出我们的author属性已经被替换成功;