在runtime中祝懂,屬性property具有一系列特性attribute顽耳,如下是attribute的數(shù)據(jù)結(jié)構(gòu)
typedef struct {
const char *name; /**< 特姓名*/
const char *value; /**< 特性值(通常為空) */
} objc_property_attribute_t;
在runtime中取得屬性特性的函數(shù)為
objc_property_attribute_t *property_copyAttributeList(objc_property_t property, unsigned int *outCount)
這個(gè)函數(shù)返回的是屬性的特性數(shù)組
同樣還能使用函數(shù)
const char *property_getAttributes(objc_property_t property)
獲得關(guān)于屬性特性描述的C字符串坠敷,在這個(gè)字符串中:
- 每個(gè)特性都以逗號“,”分隔射富,特性名和特性值前后連接
- 字符串以屬性類型開頭膝迎,以對應(yīng)成員變量名稱結(jié)尾
- 特性名一般使用屬性類型編碼表示,大多數(shù)特性的只有特性名胰耗,其特性值通常為空
例如限次,如下類
@interface Model : NSObject
@property NSString *str2;
@property (nonatomic, copy)NSDictionary *dic1;
@end
具有兩個(gè)屬性str2與dic1,其屬性的特性描述分別為
2016-08-06 16:05:46.625 RuntimeExercise[1102:87334] 屬性str2的特性描述為T@"NSString",&,V_str2
2016-08-06 16:05:46.625 RuntimeExercise[1102:87334] 屬性dic1的特性描述為T@"NSDictionary",C,N,V_dic1
按照特性描述可以看到屬性str2有三個(gè)屬性
特性1: name:T value:@"NSString"
特性2: name:& value:空
特性3: name:V value:_str2
而屬性dic1則有四個(gè)屬性柴灯,因?yàn)閐ic1指定了屬性選項(xiàng)和非原子性卖漫,而str2僅默認(rèn)屬性選項(xiàng)strong
特性1: name:T value:@"NSDictionary"
特性2: name:C value:空
特性3: name:N value:空
特性4: name:V value:_dic1
其中T费尽、C、N懊亡、V是屬性類型編碼依啰,因此可以總結(jié)出常用的特性
屬性類型: name:T value:類型名
屬性選項(xiàng): name:&、C店枣、W value:空
原子性: name:N value:空
對應(yīng)成員變量: name:V value:成員變量名
示例代碼如下:
@interface Model : NSObject
@property NSString *str2;
@property (nonatomic, copy)NSDictionary *dic1;
@end
@implementation Model
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
unsigned int outCount = 0;
objc_property_t *properties = class_copyPropertyList([Model class], &outCount);
for(int i = 0; i < outCount; i++)
{
objc_property_t property = properties[i];
const char *name = property_getName(property);
const char *attri = property_getAttributes(property);
NSLog(@"屬性%s的特性描述為%s", name, attri);
unsigned int attrCount = 0;
objc_property_attribute_t *attrs = property_copyAttributeList(property, &attrCount);
for(int i = 0; i < attrCount; i++)
{
objc_property_attribute_t attr = attrs[i];
const char *name = attr.name;
const char *value = attr.value;
NSLog(@"name:%s value:%s",name,value);
}
free(attrs)
NSLog(@"\n");
}
free(properties);
}
return 0;
}
運(yùn)行結(jié)果:
2016-08-06 16:40:30.171 RuntimeExercise[1185:99954] 屬性str2的特性描述為T@"NSString",&,V_str2
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] name:T value:@"NSString"
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] name:& value:
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] name:V value:_str2
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954]
2016-08-06 16:40:30.172 RuntimeExercise[1185:99954] 屬性dic1的特性描述為T@"NSDictionary",C,N,V_dic1
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:T value:@"NSDictionary"
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:C value:
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:N value:
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954] name:V value:_dic1
2016-08-06 16:40:30.173 RuntimeExercise[1185:99954]
Program ended with exit code: 0
有關(guān)屬性以及屬性編碼知識速警,可以閱讀相關(guān)官方文檔-Declared Properties