0
点赞
收藏
分享

微信扫一扫

造轮子 - 将对象的属性值初始化赋值

勇敢乌龟 2021-09-23 阅读 163

//获取对象的所有属性,并将对象中的所有属性赋值为空,约束,需要先讲属性初始化一次,然后赋值
-(NSDictionary*)getAllProperties:(id)objectModel{

//创建字典 将属性放入字典key值中,然后将key值所对应的value值置为 空
NSMutableDictionary *allKeyDic = [NSMutableDictionary dictionary];
u_int count;
objc_property_t *properties = class_copyPropertyList([objectModel class], &count);
NSMutableArray *propertiesArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count; i ++) {
    const char* propertyName = property_getName(properties[i]);
    
    [propertiesArray addObject:[NSString stringWithUTF8String:propertyName]];
}
free(properties);
for (NSString *str in propertiesArray) {
    NSString *value = @"";
    [allKeyDic setObject:value forKey:str];
}
return allKeyDic;

}

不足之处,接受任何形式的批评

举报

相关推荐

0 条评论