標籤:
前言
在學習解析資料的時候,我們經常是這麼寫的:
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);
輸出結果:
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
看上去很有條理,按部就班,但是一旦資料多了起來,卻會非常繁瑣,所以這次我會介紹一個相對輕鬆的方法setValuesForKeysWithDictionary。
簡單使用
如果用setValuesForKeysWithDictionary這個方法會怎樣?
將賦值過程
test.name=dic[@"name"]; test.sex=dic[@"sex"]; test.age=dic[@"age"];
替換為一句話
[test setValuesForKeysWithDictionary:dic];
輸出結果一模一樣,是不是簡單又方便?
深入的問題
- 如果model裡面的有不存在於dic中的元素會怎樣?
在Model檔案中添加一行
@property (nonatomic,copy)NSString *other;
並輸出得時候輸出
NSLog(@"test.other=%@",test.other);
輸出結果:
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)
顯而易見,dic中得值可以完全賦值給model,而other沒有被賦值,所以值是空的。
2.如果dic裡面的有不存在於model中的元素會怎樣?
在Model檔案中刪除一行
@property (nonatomic,copy) NSString* age;
在刪除對應得輸出後運行。
糟了!通過了編譯,但是運行時報錯!
Terminating app due to uncaught exception ‘NSUnknownKeyException‘,
reason: ‘[<PersonModel 0x7fd731517910> setValue:forUndefinedKey:]:
this class is not key value coding-compliant for the key age.‘
因為在model中,沒有對應的age屬性,所以導致了程式崩潰。
解決方式就是實現一個方法setValue:forUndefinedKey: 這個方法能過濾掉不存在的索引值。
在model中添加。
h檔案中添加:
-(void)setValue:(id)value forUndefinedKey:(NSString *)key;
並需要在m檔案中實現:
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{ }
對,並不需要在方法中寫任何內容。
現在來運行一下。
輸出結果:
2015-10-19 13:55:55.390
setValuesForKeysWithDictionary[10082:937173] test.name=張三
2015-10-19 13:55:55.391
setValuesForKeysWithDictionary[10082:937173] test.sex=男
成功運行!
3.如果dic中的key與model中的變數名字不同,應該怎麼賦值?
從前面我們可以知道,dic中key賦值給model中與key同名的屬性。
那麼如果dic中得key值為 username,model中的名字為name,又或是dic中的key值為ID,INT 等關鍵字,應該怎麼變化。
答案也是從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; } }
運行後結果:
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.jianshu.com/p/870eb4b4170a
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。
【iOS開發】字典的快速賦值 setValuesForKeysWithDictionary