Use of Objective-c kvc and KVO, objective-ckvckvo
Use of KVC
In general, we use attributes or access methods to access instance variables. However, we can also use Key-Value-Coding (KVC) the value of the instance variable to be accessed by key-value encoding.
KVC process:
First, we define a Person class. The Code is as follows:
/**Person.h*/
#import <Foundation/Foundation.h>
@interface Person : NSObject
{ NSString *_name;}@end/**Person.m*/
#import "Person.h"
@implementation Person@end
Create an instance of the Person class and use KVC to assign values to the variable of the _ name instance:
# Import <Foundation/Foundation. h> # import "Person. h "int main (int argc, const char * argv []) {@ autoreleasepool {Person * person = [[Person alloc] init];
// Assign the instance variable _ name of person to SmithJackyson [person setValue: @ "SmithJackyson" forKey: @ "_ name"];
// Retrieve the NSString * name = [person valueForKey: @ "_ name"]; NSLog (@ "name = % @", name );} return 0 ;}
The output is as follows:
20:42:14. 098 test [1559: 113739] name = SmithJackyson
Program ended with exit code: 0
If the attribute of Class A is A Class B, A Key Path is required to access the attribute of Class B.
Assume that the Student class has a Course class attribute _ course. The Course class has an attribute that indicates the Course name NSString * _ courseName;
The following uses a Key Path to access attributes:
# Import <Foundation/Foundation. h> # import "Student. h "# import" Course. h "int main (int argc, const char * argv []) {@ autoreleasepool {// Note: KVC is used to access instance variables for instances, so Initialization is required, assign the value to the _ course instance variable of student to course; otherwise, the value cannot be correctly assigned to Student * student = [[Student alloc] init]; Course * course = [[Course alloc] init]; [student setValue: course forKey: @ "_ course"]; // assign a value to the attribute _ courseName of the student instance variable _ course [student setValue: @ "English" forKeyPath: @ "_ course. _ courseName "]; // obtain the value of the _ courseName attribute of the student instance variable _ course NSString * courseName = [student valueForKeyPath: @" _ course. _ courseName "]; NSLog (@" courseName = % @ ", courseName);} return 0 ;}
The following is the result:
21:15:59. 094 test [1867: 123245] courseName = English
Program ended with exit code: 0
If the attribute is of the basic data type, you can use the string value to directly assign a value to the attribute, and use the string variable to accept the value.
Use of KVO
KVO, that is, key-value observing, provides A mechanism. Assume that object A observes the specified attribute C of object B. When the value of attribute C of object B changes, KVO notifies the corresponding observer A that object A can perform some operations, such as updating the view.
Usage process:
First, define a Person class. The Code is as follows:
#import <Foundation/Foundation.h>@interface Person : NSObject@property (nonatomic,copy)NSString *name;- (instancetype)initWithName:(NSString *)name;@end#import "Person.h"@implementation Person- (instancetype)initWithName:(NSString *)name{ self = [super init]; if (self) { _name = name; } return self;}@end
Then use KVO in ViewController. The Code is as follows:
# Import "ViewController. h "# import" Person. h "@ interface ViewController () {Person * person;} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; person = [[Person alloc] initWithName: @ "John"]; // Add the observer as ViewController for the property name of the person object [person addObserver: self forKeyPath: @ "name" options: NSKeyValueObservingOptionNew context: nil]; // you can change the name value in the following two ways, and the callback method person is triggered. name = @ "SmithJackyson"; [person setValue: @ "SmithJackyson" forKey: @ "name"] ;}// implements the KVO callback method, as long as the property name of person changes, the method-(void) observeValueForKeyPath :( NSString *) keyPath ofObject :( id) object change :( NSDictionary <NSString *, id> *) change context :( void *) context {
// The content stored in the change dictionary is determined by the option you selected when registering. If it is NSKeyValueObservingOptionNew, the dictionary stores the changed value, and the key is new, similarly, NSKeyValueObservingOptionOld is stored in the dictionary to change the previous value, and the key is old. if both are available, the dictionary stores two values if ([keyPath is1_tostring: @ "name"] & object = person) {NSLog (@ "now name = % @", person. name) ;}}-(void) didreceivemorywarning {[super didreceivemorywarning]; // Dispose of any resources that can be recreated .} @ end
The running result is as follows:
16:17:29. 102 test [1752: 94898] now name = SmithJackyson
16:17:29. 102 test [1752: 94898] now name = SmithJackyson
Finally, you need to remove the observer.
- (void)dealloc{ [person removeObserver:self forKeyPath:@"name"];}
Reprinted Please note: Author SmithJackyson