您的位置:首页 > 产品设计 > UI/UE

objc_property_attribute_t 的value和name

2016-03-06 21:04 344 查看
获取一个属性的attributes

@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

常用attributenamevalue
nonatomic"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 
动态添加属性

@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);
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: