IOS KVC (Key-Value Coding), kvckey-value
Common usage:
- Get value
- ValueForKey: Value Based on attribute name
- ValueForKeyPath: Specifies the PATH value (for example:[Person valueForKeyPath: @ "car. price"])
- ValueForUndefinedKey throws an exception by default. You can rewrite this function for error handling (rarely used)
- Modify Value
- SetValue: forKey: Set Value Based on Attribute
- SetValue: forKeyPath: set the value based on the path
- SetValue: forUndefinedKey:
- SetNilValueForKey: When nil is set for non-Class Object Attributes, an exception is thrown by default.
- Dictionary-to-Model
- SetValuesForKeysWithDictionary: dictionary-to-Model
Code:
Define a HSCar class
//// HSCar. h // KVC // Created by hans on 15/7/13. // Copyright 2015 hans. all rights reserved. // # import <Foundation/Foundation. h> @ interface HSCar: NSObject/** name */@ property (nonatomic, copy) NSString * name;/** price */@ property (nonatomic, assign) double price; @ end
Define a HSBook class
//// HSBook. h // KVC // Created by hans on 15/7/13. // Copyright 2015 hans. all rights reserved. // # import <Foundation/Foundation. h> @ interface HSBook: NSObject/** title */@ property (nonatomic, copy) NSString * name;/** price */@ property (nonatomic, assign) double price; @ end
Define a heat map class
//// 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/** name */@ property (nonatomic, copy) NSString * name;/** age */@ 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 {// weight double _ weight;}-(void) showWeight {NSLog (@" weight: % f ", _ weight);} @ end
Implemented in 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 = @ "Lamborghini"; person. car. price = 10000000.0;/** valueForKey: */NSLog (@ "name: % @ age: % @", [person valueForKey: @ "name"], [person valueForKey: @ "age"]); // 22:32:02. 356 KVC [54356: 872650] name: hans age: 23/** valueForKeyPath: */NSLog (@ "name: % @ price: % @", [person valueForKeyPath: @ "car. name "], [person valueForKeyPath: @" car. price "]); // 22:51:27. 075 KVC [54669: 885579] name: Lamborghini price: 10000000/** setValue: forKey: */[person setValue: @ "hanshhh" forKey: @ "name"]; [person setValue: @ "100" forKey: @ "age"]; NSLog (@ "name: % @ age: % @", [person valueForKey: @ "name"], [person valueForKey: @ "age"]); // 22:53:54. 876 KVC [54709: 886954] name: hanshhh age: 100/** setValue: forKeyPath: */[person setValue: @ "Ferrari" forKeyPath: @ "car. name "]; [person setValue: @ (9999999) forKeyPath: @" car. price "]; NSLog (@" name: % @ price: % @ ", [person valueForKeyPath: @" car. name "], [person valueForKeyPath: @" car. price "]); // 22:56:36. 336 KVC [54767: 888926] name: Ferrari price: 9999999/** change private attributes */[person setValue: @ (120) forKeyPath: @ "_ weight"]; [person showWeight]; // 23:01:15. 151 KVC [54847: 892122] weight: 120.000000/** dictionary-to-model * // 1. simple conversion of NSDictionary * dict1 ={ @ "name": @ "Hans", @ "age": @ "23" ,}; [person setValuesForKeysWithDictionary: dict1]; NSLog (@ "name: % @, age: % d", person. name, person. age); // 23:04:20. 298 KVC [54943: 895092] name: Hans, age: 23 // 2. complex model conversion (Note: person. A car cannot be converted and will become a dictionary object. It is actually a simple pointer assignment.) NSDictionary * dict2 = @ {@ "name": @ "Hans hahaha", @ "age ": @ "23", @ "car": @ {@ "name": @ "Lamborghini", @ "price": @ "10000000" },}; [person setValuesForKeysWithDictionary: dict2]; NSLog (@ "% @", person. car); // 23:10:32. 310 KVC [55019: 899474] {// name = "\ U5170 \ U535a \ U57fa \ U5c3c"; // price = 10000000; ///}/** other operations */HSBook * book1 = [HSBook new]; book1.name = @ ""; book1.price = 51.0; HSBook * book2 = [HSBook new]; book2.name = @ "out of control"; 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 "]); // 23:20:43. 434 KVC [55171: 905643] 3 // 23:20:43. 434 KVC [55171: 905643] 50.333333333333333333333333333333333333 // 23:20:43. 435 KVC [55171: 905643] 40 // 23:20:43. 435 KVC [55171: 905643] 60}-(void) didReceiveMemoryWarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.