KVO
-
KVO whole-process key-value-observing, commonly known as “key-value monitoring”, can be used to monitor an object property worth changing
-
Simple use of KVO
#import "ViewController.h" #import "MJPerson.h" @interface ViewController () @property (strong, nonatomic) MJPerson *person1; @property (strong, nonatomic) MJPerson *person2; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.person1 = [[MJPerson alloc] init]; self.person1.age = 1; self.person1.height = 11; self.person2 = [[MJPerson alloc] init]; self.person2.age = 2; self.person2.height = 22; / / to add KVO monitoring objects person1 NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld; [self.person1 addObserver:self forKeyPath:@"age" options:options context:@"123"]; [self.person1 addObserver:self forKeyPath:@"height" options:options context:@"456"]; } - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { self.person1.age = 20; self.person2.age = 20; self.person1.height = 30; self.person2.height = 30; } - (void)dealloc { [self.person1 removeObserver:self forKeyPath:@"age"]; [self.person1 removeObserver:self forKeyPath:@"height"]; } // When the listener's property value changes, - (void) observeforkeypath :(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {NSLog(@" monitoring %@ - %@ - %@", object, keyPath, change, context); } @end #import <Foundation/Foundation.h> @interface MJPerson : NSObject @property (assign, nonatomic) int age; @property (assign, nonatomic) int height; @endCopy the code
- Objects that are not monitored by KVO
- The object that KVO listens on
-
Isa Pointers to KVO listening instances point to NSKVONotifying_XXX class objects
-
The ISA pointer to an instance object that is not listening on by KVO points to an object of class XXX
-
NSKVONotifying_XXX class internal method call conjecture
- Call to order
- Check for the presence of _NSSet *AndNotify
- Internal implementation of _NSSet *ValueAndNotify
KVC
- KVC stands for key-value Coding, commonly known as “key-value Coding”, which allows access to an attribute through a Key
- The common apis are
-
(void)setValue:(id)value forKeyPath:(NSString *)keyPath;
-
(void)setValue:(id)value forKey:(NSString *)key;
-
(id)valueForKeyPath:(NSString *)keyPath;
-
(id)valueForKey:(NSString *)key;
-
- The principle of setValue: forKey:
- Call to order
- 1. Find and call setKey: method. If no, find and call _setKey: pass the value to call method
- 2. The two methods are not just find accessInstanceVariableDirecity the return value is Yes or No
- 3.Yes Check whether _key _iskey key isKey and assign values to them
- 4.No or No corresponding member variable is reported as No such member variable
- The principle of valueForKey:
- Call sequential similar to above
- 1. Find and call getKey:, key:, isKey, _key, pass the value to call the method
- 2. The two methods are not just find accessInstanceVariableDirecity the return value is Yes or No
- 3.Yes Checks whether _key _ISkey key and isKey member variables exist in sequence
- 4.No or No corresponding member variable is reported as No such member variable