Http://www.cnblogs.com/QM80/p/3647819.html
If you want to modify the property values of an object
1. In general, use the set method of the object property directly to modify:
Student *stu = [[Student alloc] init];
The two writing formats of the Set method [Stu Setage:10]; Stu.age = 10;
2. But what if I don't know the object type? Then you can use the KVC key value encoding (key value Coding) to indirectly modify the object properties
The KVC is implemented by using strings to describe the properties that an object needs to modify.
Basic calls to KVC include: valueforkey: and Setvalue:forkey: sending a message to an object as a string
KVC General Usage:
/******************************** Create the Book.h file just to illustrate Forkeypath usage without implementing *********************************/#import <Foundation/Foundation.h> @interface Book:nsobject@property (nonatomic,assign) double price; The price of the book @end
/******************************** Student.h file *********************************/#import <foundation/ Foundation.h> @class Book, @interface student:nsobject@property (nonatomic,assign) int age; Student Age @property (nonatomic,copy) nsstring *name; Student name @property (nonatomic,retain) book *book; Students have a book//test Method-(void) test; @end
/******************************** student.m file *********************************/#import "Student.h" #import "Book.h "@implementation student-(void) test{Student *stu = [[Student alloc] init];//1. Assign a single value to the basic data type[Stu setvalue:@ "John" forkey:@ "name"]; NSString *str = [stu valueforkey:@ "name"]; str = John//2. Assigning a single value to an object typeSetValue you want the parameter to be an ID type, first wrap the base data type as an object type [stu setvalue:@10 forkey:@ "age"]; After the object is removed according to the key value, the conversion becomes the basic data type int age = [[Stu valueforkey:@ ' age '] intvalue]; Ten NSLog (@ "%@%d", str,age); John 10//3. The batch is assigned a value for the base data type and object type[Stu setvaluesforkeyswithdictionary:@{@ "age": @20,@ "name": @ "Jim"}]; Extract all values according to key into dictionary nsdictionary *dic = [stu dictionarywithvaluesforkeys:@[@ "name" @ "age"]; NSLog (@ "%@", DIC); Age = 20;name = Jim//4. Indirectly assign a value to the price property of the book objectStu.book = [[Book alloc] init]; Create a Stu.book object
First way: Direct assignment [Stu.book setprice:20.00]; Price = 20.00//The second way: assigns a value by the key value [Stu.book setvalue:@30.00 forkey:@ "Price"]; Price = 30.00//The Third way: Assign values through the path of the Jian [Stu setvalue:@40 forkeypath:@ "Book.price"]; Price = 40.00//Key and key path is equivalent to file name and file path name, then the key value path is a key value, so you can use the key value can be used when the key path instead of the second way can also be written as [Stu.book setvalue:@50 forkeypath:@ "Price"]; Price = 50.00 NSLog (@ "%.2f", Stu.book.price); Test output} @end
In addition: KVC also provides methods for manipulating arrays and some calculated parameters
2.KVO (key value observing) key-value observation mechanism, mainly used to monitor the change of object properties
Implementation: Adding listeners
Example: Teacher class to listen for changes in the value of the Name property in the student class
/***************************** Student.h file *************************************/#import <foundation/ Foundation.h> @interface Student:nsobject@property (nonatomic,copy) NSString *name; Declares the listener's properties//Test listener Method-(void) test; @end
/***************************** Student.h file *************************************/#import "Student.h" #import " Teacher.h "@implementation student-(void) test{ Student *stu = [[Student alloc] init]; Use KVC to assign a value to the name attribute [Stu setvalue:@ "chapter three" forkey:@ "name"]; //Implement teacher Class listener student property name change 1. Create listener object Teacher *teacher = [[Teacher alloc] init]; Addoberver is the NSObject classification method then any object can add a listening method //2. Adding Listener objects to student class teacher options parameter: Listening for new or old values //Forkeypath: Key Path Context: Contexts used in animations [Stu addobserver:teacher forkeypath:@ "name" Options:nskeyvalueobservingoptionnew Context:nil ]; Only the new values are monitored here, of course, you can listen together Option:nskeyvalueobservingoptionnew | Nskeyvalueobservingoptionold //Modify the value of the Student Class Name property to notify the Listener teacher object and then call the //Listener property in the teacher class after the value of the object has changed The method of executing the listener } @end
/***************************** Teacher.h file *************************************/#import <foundation/ Foundation.h> @interface Teacher:nsobject@end
/***************************** teacher.m file *************************************/#import "Teacher.h" #import " Student.h "@implementation Teacher//Once the supervisor hears that the Name property value in the Student class is changed, the listener is called in the Teacher class using the parameter description //KeyPath = @" Name "; Object is the Student class change: changes to whether the value is a new value or old value or the new old value has a corresponding is the option parameter in the Listener-(void) Observevalueforkeypath: (NSString *) keypath Ofobject: (ID) object change: (nsdictionary *) Change context: (void *) context{ NSLog (@ "keypath:%@", keypath); Keypath:name NSLog (@ "objcet:%@", object); Object:student NSLog (@ "change:%@", change); \u674e\u65af characters are escaped} @end
/***************************** mian.m file *************************************/#import <foundation/foundation.h > #import "Student.h" int main (int argc, const char * argv[]) { @autoreleasepool { //test Student *stu = [[Stud ENT alloc] init]; [Stu Test]; } return 0;}