使用KVC对person的属性进行赋值
[aperson setValue:@"yadong" forKey:@"name"];
[aperson setValue:@"man" forKey:@"sex"];
[aperson setValue:@23 forKey:@"age"];
NSLog(@"%@",[aperson valueForKey:@"name"]); // aperson.name
使用键值路径复合赋值
// aperson setValue:<#(id)#> forKeyPath:<#(NSString *)#>
// 创建 person 对象
Person *aperson = [[Person alloc] init];
// 使用字典对person的属性进行赋值(熟练掌握)
NSDictionary *dic = @{
@"name":@"yadong",
@"sex":@"man",
@"age":@22
};
// 赋值
[aperson setValuesForKeysWithDictionary:dic];
NSLog(@"%@",aperson);
解析 plist 文件,主要看root,是什么建什么
NSString *patch = [[NSBundle mainBundle] pathForResource:@"Person" ofType:@"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:patch];
// NSLog(@"%@", array);
// 把字典转化为person模型
NSMutableArray *personArray = [NSMutableArray array];
for (NSDictionary *dict in array) {
Person *p = [[Person alloc] init];
[p setValuesForKeysWithDictionary:dict];
[personArray addObject:p];
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/yadong_zhao/article/details/46817261