造輪子 - 將對象的屬性值初始化賦值

初始化一個model的數據模型得到的效果:


屏幕快照 2017-01-09 18.15.24.png

//獲取對象的所有屬性,并將對象中的所有屬性賦值為空,約束,需要先講屬性初始化一次,然后賦值
-(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;

}

使用該方法之后的效果,如下圖:


屏幕快照 2017-01-09 18.16.21.png

不足之處,接受任何形式的批評

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容