前言
在學(xué)習(xí)解析數(shù)據(jù)的時(shí)候,我們經(jīng)常是這么寫(xiě)的:
PersonModel.h文件中
@property(nonatomic,copy)NSString*name;@property(nonatomic,copy)NSString*sex;@property(nonatomic,copy)NSString*age;
字典:
NSDictionary*dic = @{@"name":@"張三",@"sex":@"男",@"age":@"22"};
賦值:
PersonModel *test=[[PersonModel alloc]init];? ? ? ? test.name=dic[@"name"];? ? ? ? test.sex=dic[@"sex"];? ? ? ? test.age=dic[@"age"];
輸出:
NSLog(@"test.name=%@",test.name);NSLog(@"test.sex=%@",test.sex);NSLog(@"test.age=%@",test.age);
輸出結(jié)果:
2015-10-19 13:31:25.478
setValuesForKeysWithDictionary[9676:913009] test.name=張三
2015-10-19 13:31:25.478
setValuesForKeysWithDictionary[9676:913009] test.sex=男
2015-10-19 13:31:25.478
setValuesForKeysWithDictionary[9676:913009] test.age=22
看上去很有條理,按部就班榨崩,但是一旦數(shù)據(jù)多了起來(lái)歼捐,卻會(huì)非常繁瑣因悲,所以這次我會(huì)介紹一個(gè)相對(duì)輕松的方法setValuesForKeysWithDictionary嘶居。
簡(jiǎn)單使用
如果用setValuesForKeysWithDictionary這個(gè)方法會(huì)怎樣?
將賦值過(guò)程
test.name=dic[@"name"];? ? test.sex=dic[@"sex"];? ? test.age=dic[@"age"];
替換為一句話
[testsetValuesForKeysWithDictionary:dic];
輸出結(jié)果一模一樣,是不是簡(jiǎn)單又方便躁劣?
深入的問(wèn)題
如果model里面的有不存在于dic中的元素會(huì)怎樣?
在Model文件中添加一行
@property(nonatomic,copy)NSString*other;
并輸出得時(shí)候輸出
NSLog(@"test.other=%@",test.other);
輸出結(jié)果:
2015-10-19 13:49:25.955
setValuesForKeysWithDictionary[9964:928391] test.name=張三
2015-10-19 13:49:25.956
setValuesForKeysWithDictionary[9964:928391] test.sex=男
2015-10-19 13:49:25.956
setValuesForKeysWithDictionary[9964:928391] test.age=22
2015-10-19 13:49:25.956
setValuesForKeysWithDictionary[9964:928391] test.other=(null)
顯而易見(jiàn),dic中得值可以完全賦值給model库菲,而other沒(méi)有被賦值账忘,所以值是空的。
2.如果dic里面的有不存在于model中的元素會(huì)怎樣熙宇?
在Model文件中刪除一行
@property(nonatomic,copy)NSString* age;
在刪除對(duì)應(yīng)得輸出后運(yùn)行鳖擒。
糟了!通過(guò)了編譯烫止,但是運(yùn)行時(shí)報(bào)錯(cuò)!
Terminating app due to uncaught exception 'NSUnknownKeyException',
reason: '[ setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key age.'
因?yàn)樵趍odel中蒋荚,沒(méi)有對(duì)應(yīng)的age屬性,所以導(dǎo)致了程序崩潰馆蠕。
解決方式就是實(shí)現(xiàn)一個(gè)方法setValue:forUndefinedKey: 這個(gè)方法能過(guò)濾掉不存在的鍵值期升。
在model中添加。
h文件中添加:
-(void)setValue:(id)value forUndefinedKey:(NSString*)key;
并需要在m文件中實(shí)現(xiàn):
-(void)setValue:(id)value forUndefinedKey:(NSString*)key{? ? ? ? }
對(duì)互躬,并不需要在方法中寫(xiě)任何內(nèi)容播赁。
現(xiàn)在來(lái)運(yùn)行一下。
輸出結(jié)果:
2015-10-19 13:55:55.390
setValuesForKeysWithDictionary[10082:937173] test.name=張三
2015-10-19 13:55:55.391
setValuesForKeysWithDictionary[10082:937173] test.sex=男
成功運(yùn)行吼渡!
3.如果dic中的key與model中的變量名字不同容为,應(yīng)該怎么賦值?
從前面我們可以知道寺酪,dic中key賦值給model中與key同名的屬性坎背。
那么如果dic中得key值為 username,model中的名字為name寄雀,又或是dic中的key值為ID,INT 等關(guān)鍵字得滤,應(yīng)該怎么變化。
答案也是從setValue:forUndefinedKey方法入手盒犹。
首先我們把dic的值改變:
NSDictionary*dic = @{@"username":@"張三",@"sex":@"男",@"id":@"22"};
model中的屬性:
@property(nonatomic,copy)NSString*name;@property(nonatomic,copy)NSString*sex;@property(nonatomic,copy)NSString* age;
完善model中的setValue:forUndefinedKey方法
-(void)setValue:(id)value forUndefinedKey:(NSString*)key{if([key isEqualToString:@"id"])? ? ? ? {self.age=value;? ? ? ? }if([key isEqualToString:@"username"])? ? ? ? {self.name=value;? ? ? ? }? ? }
運(yùn)行后結(jié)果:
? ? 2015-10-19 14:30:11.241
? ? setValuesForKeysWithDictionary[10289:956012] test.name=張三
? ? 2015-10-19 14:30:11.242
? ? setValuesForKeysWithDictionary[10289:956012] test.sex=男
? ? 2015-10-19 14:30:11.242
? ? setValuesForKeysWithDictionary[10289:956012] test.age=22
正常輸出耿戚!
作者:嘻嘻zhy
鏈接:http://www.reibang.com/p/870eb4b4170a
來(lái)源:簡(jiǎn)書(shū)
簡(jiǎn)書(shū)著作權(quán)歸作者所有湿故,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。