iOS KVC(Key-Value Coding)
常見使用方法:擷取值
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 @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 @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 @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