常见用法:
- 获取值
- valueForKey: 依据属性名取值
- valueForKeyPath: 依据路径取值(如:[person valueForKeyPath:@”car.price”])
- valueForUndefinedKey 默认实现是抛出异常。能够重写这个函数做错误处理 ( 比較少用 )
- 改动值
- setValue:forKey: 依据属性设值
- setValue:forKeyPath: 依据路径设值
- setValue:forUndefinedKey:
- setNilValueForKey: 当对非类对象属性设置nil时。调用。默认抛出异常
- 字典转模型
- setValuesForKeysWithDictionary: 字典转模型
上代码:
定义一个HSCar类
//
// HSCar.h
// KVC
//
// Created by hans on 15/7/13.
// Copyright © 2015年 hans. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HSCar : NSObject
/** 名字 */
@property (nonatomic, copy) NSString *name;
/** 价格 */
@property (nonatomic, assign) double price;
@end
定义一个HSBook类
//
// HSBook.h
// KVC
//
// Created by hans on 15/7/13.
// Copyright © 2015年 hans. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HSBook : NSObject
/** 书名 */
@property (nonatomic, copy) NSString *name;
/** 价格 */
@property (nonatomic, assign) double price;
@end
定义一个HSPerson类
//
// HSPerson.h
// KVC
//
// Created by hans on 15/7/13.
// Copyright © 2015年 hans. All rights reserved.
//
#import <Foundation/Foundation.h>
@class HSCar;
@interface HSPerson : NSObject
/** 名字 */
@property (nonatomic, copy) NSString *name;
/** 年龄 */
@property (nonatomic, copy) NSString *age;
/** 车 */
@property (nonatomic, strong) HSCar *car;
@end
//
// HSPerson.m
// KVC
//
// Created by hans on 15/7/13.
// Copyright © 2015年 hans. All rights reserved.
//
#import "HSPerson.h"
@implementation HSPerson
{
// 体重
double _weight;
}
- (void)showWeight
{
NSLog(@"weight: %f", _weight);
}
@end
在ViewController实现
//
// ViewController.m
// KVC
//
// Created by hans on 15/7/13.
// Copyright © 2015年 hans. All rights reserved.
//
#import "ViewController.h"
#import "HSPerson.h"
#import "HSCar.h"
#import "HSBook.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
HSPerson *person = [HSPerson new];
person.name = @"hans";
person.age = 23;
person.car = [HSCar new];
person.car.name = @"兰博基尼";
person.car.price = 10000000.0;
/** valueForKey: */
NSLog(@"name: %@ age: %@", [person valueForKey:@"name"], [person valueForKey:@"age"]);
// 2015-07-13 22:32:02.356 KVC[54356:872650] name: hans age: 23
/** valueForKeyPath: */
NSLog(@"name:%@ price: %@", [person valueForKeyPath:@"car.name"], [person valueForKeyPath:@"car.price"]);
// 2015-07-13 22:51:27.075 KVC[54669:885579] name:兰博基尼 price: 10000000
/** setValue:forKey: */
[person setValue:@"hanshhh" forKey:@"name"];
[person setValue:@"100" forKey:@"age"];
NSLog(@"name: %@ age: %@", [person valueForKey:@"name"], [person valueForKey:@"age"]);
// 2015-07-13 22:53:54.876 KVC[54709:886954] name: hanshhh age: 100
/** setValue:forKeyPath: */
[person setValue:@"法拉利" forKeyPath:@"car.name"];
[person setValue:@(9999999) forKeyPath:@"car.price"];
NSLog(@"name:%@ price: %@", [person valueForKeyPath:@"car.name"], [person valueForKeyPath:@"car.price"]);
// 2015-07-13 22:56:36.336 KVC[54767:888926] name:法拉利 price: 9999999
/** 更改私有属性 */
[person setValue:@(120) forKeyPath:@"_weight"];
[person showWeight];
// 2015-07-13 23:01:15.151 KVC[54847:892122] weight: 120.000000
/** 字典转模型 */
// 1.简单转换
NSDictionary *dict1 = @{
@"name" : @"汉斯",
@"age" : @"23",
};
[person setValuesForKeysWithDictionary:dict1];
NSLog(@"name : %@, age : %d", person.name, person.age);
// 2015-07-13 23:04:20.298 KVC[54943:895092] name : 汉斯, age : 23
// 2.复杂模型转换(注意:person.car不能转换。会变为字典对象,事实上就是简单的指针赋值而已)
NSDictionary *dict2 = @{
@"name" : @"汉斯哈哈哈",
@"age" : @"23",
@"car" : @{
@"name" : @"兰博基尼",
@"price" : @"10000000"
},
};
[person setValuesForKeysWithDictionary:dict2];
NSLog(@"%@", person.car);
// 2015-07-13 23:10:32.310 KVC[55019:899474] {
// name = "U5170U535aU57faU5c3c";
// price = 10000000;
// }
/** 其它运算 */
HSBook *book1 = [HSBook new];
book1.name = @"结网";
book1.price = 51.0;
HSBook *book2 = [HSBook new];
book2.name = @"失控";
book2.price = 40.0;
HSBook *book3 = [HSBook new];
book3.name = @"异类";
book3.price = 60.0;
person.books = @[book1, book2, book3];
NSLog(@"%@", [person.books valueForKeyPath:@"@count"]);
NSLog(@"%@", [person.books valueForKeyPath:@"@avg.price"]);
NSLog(@"%@", [person.books valueForKeyPath:@"@min.price"]);
NSLog(@"%@", [person.books valueForKeyPath:@"@max.price"]);
// 2015-07-13 23:20:43.434 KVC[55171:905643] 3
// 2015-07-13 23:20:43.434 KVC[55171:905643] 50.333333333333333333333333333333333333
// 2015-07-13 23:20:43.435 KVC[55171:905643] 40
// 2015-07-13 23:20:43.435 KVC[55171:905643] 60
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end