iOS 一一 KVC實現字典轉模型

2021-08-15 02:12:32 字數 3691 閱讀 7901

一、使用kvc實現字典轉模型

1. 載入plist檔案,將plist檔案轉為字典

2. 字典轉模型

注意:使用kvc的setvaluesforkeyswithdictionary: 方法為模型屬性賦值時, 必須保證模型的屬性名和plist中的屬性名完全相同,否則會報 setvalue:forundefinedkey: 錯誤.

當plist檔案中有很多屬性,我們在模型中的屬性名也要和plist檔案中的屬性名一一對應,這樣寫模型屬性的時候會非常麻煩,因為模型中的屬性名和plist檔案中的key有聯絡,因此我們給 nsdictionary寫乙個自動生成屬性的分類.

這樣做的好處: 1. 防止模型的屬性和plist檔案中的key名寫錯,然後報錯

3. kvc原理

setvaluesforkeyswithdictionary:

方法底層①②:

① 遍歷字典中所有的key,去模型中查詢有沒有對應的屬性

② 去模型中查詢有沒有對應的屬性

[dict enumeratekeysandobjectsusingblock:^(id  _nonnull key, id  _nonnull value, bool * _nonnull stop) ];
setvalue:forkey:方法底層做的事情

[item setvalue:@"筆記" forkey:@"sourse"];

① 首先去模型中查詢有沒有setsourse,找到,直接呼叫[self setsourse:@"筆記"];

② 去模型中查詢有沒有source屬性,有,直接訪問屬性賦值 sourse = value

③ 去模型中查詢有沒有_source屬性,有,直接訪問屬性賦值 _sourse = value

④ 找不到,直接報錯

setvalue:forundefinedkey:

報找不到的錯誤

注意:有時候模型中只儲存最重要的資料,導致模型的屬性和字典不能一一對應

.此時: 可以採用 

①最原始的做法 item.source = dict[@"source"]; 根據字典的key對應的value賦值給模型屬性(太繁瑣)

②使用第三方框架mjextension 字典轉模型. 框架底層runtime : 把模型中所有的屬性都遍歷出來

③還是使用kvc的方式, 當報setvalue:forundefinedkey:

錯誤,表面找不到屬性, 只需要從寫系統的setvalue:forundefinedkey:方法,

方法體中

什麼都不做即可!

nsdictionary+property 檔案

#import "nsdictionary+property.h"

@implementation nsdictionary (property)

// iskindofclass: 判斷是否是當前類或子類

// 生成屬性** => 根據字典中的所有key

- (void)createpropertycode

else if ([value iskindofclass:[nsdictionary class]])else if([value iskindofclass:nsclassfromstring(@"__nscfboolean")])else if ([value iskindofclass:[nsnumber class]])else if ([value iskindofclass:[nsarray class]])

}];nslog(@"%@",codes);

}@end

當乙個字典來呼叫

createpropertycode 方法時,就會自動生成該plist檔案中所有key型別的屬性.

viewcontroller檔案

#import "viewcontroller.h"

#import "nsdictionary+property.h"

#import "zystatusitem.h"

@inte***ce viewcontroller ()

@end

@implementation viewcontroller

- (void)viewdidload

@end

zystatusitem檔案

#import @inte***ce zystatusitem : nsobject

//@property (nonatomic, strong) nsstring *source;

@property (nonatomic, assign) nsinteger reposts_count;

@property (nonatomic, strong) nsarray *pic_urls;

@property (nonatomic, strong) nsstring *created_at;

@property (nonatomic, assign) bool isa;

@property (nonatomic, assign) nsinteger attitudes_count;

@property (nonatomic, strong) nsstring *idstr;

@property (nonatomic, strong) nsstring *text;

@property (nonatomic, assign) nsinteger comments_count;

@property (nonatomic, strong) nsdictionary *user;

+ (instancetype)itemwithdict:(nsdictionary *)dict;

@end

#import "zystatusitem.h"

@implementation zystatusitem

// 模型只儲存最重要的資料,導致模型的屬性和字典不能一一對應

+ (instancetype)itemwithdict:(nsdictionary *)dict

];

/*[item setvalue:@"來自即可筆記" forkey:@"sourse"];

1. 首先去模型中查詢有沒有setsourse,找到,直接呼叫賦值[self setsource:@"來自即刻筆記"]

2. 去模型中查詢有沒有source屬性,有,直接訪問屬性賦值 source = value

3. 去模型中查詢有沒有_source屬性,有,直接訪問屬性賦值 _source = value

4. 找不到,直接報錯 setvalue:forundefinedkey:報找不到的錯誤

*/return item;

}// 重寫系統方法? 1.想給系統方法新增額外功能 2. 不想要系統方法實現

// 系統找不到就會呼叫這個方法

- (void)setvalue:(id)value forundefinedkey:(nsstring *)key

注意:使用kvc的弊端, 當字典中巢狀字典時,或者多級巢狀的關係,使用kvc來解決非常繁瑣.因此推薦使用runtime來實現字典轉模型操作, 第三方框架mjextension底層就是使用runtime來實現的.

MCV中字典轉模型的KVC底層實現

instancetype initwithdictionary nsdictionary dict return self 遍歷字典裡面所有的key和值,name,icon enumeratekeysandobjectsusingblock 遍歷字典中的所有keys和valus dict enume...

ios 字典轉模型

property nonatomic,copy nsstring name property nonatomic,copy nsstring icon 例項化 instancetype initwithdic nsdictionary dic 類方法可以快速例項化乙個模型 end m檔案 字典例項化...

iOS 字典轉模型

instancetype initwithdict nsdictionary dict return self instancetype carswithdict nsdictionary dict 分組 inte ce azcargroup nsobject 車模型陣列 property nona...