1. Implementation principle of KVO
- through
runtime
Subclasses override properties that need to be listened on by KVO, calling NSObject’s listening methods before and after setters for that property, so that KVO implements callbacks before and after property changes. - The format of the subclass derived from KVO should be:
NSKVONotifying_ + the name of the class
Class, such asNSKVONotifying_Person
- Isa pointer structure diagram for objects not listened on by KVO
- Isa pointer structure diagram for objects that use KVO listening
_NSSetValueAndNotify
Internal implementation of
- An internal method of a subclass
class
Method hides the internal implementationNSKVONotifying_MJPerson
Classes exist, and the Runtime can get the real type
2. How do I trigger a KVO manually
- Key-value observation notifications depend on two methods of NSObject
-
willChangeValueForKey:
-
didChangeValueForKey:
- Before an observed property changes,
willChangeValueForKey:
It must be called, which records the old value. - And when that happens,
didChangeValueForKey:
Will be called, and thenobserveValueForKey:ofObject:change:context
Will also be called. - If you can implement these calls manually, you can implement “manually trigger KVO”.
3. How to set filter conditions for system KVO?
- Such as: cancel
Person
classage
Property. KVO is triggered manually when age is greater than 18
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)key { if ([key isEqualToString:@"age"]) { return NO; } return [super automaticallyNotifiesObserversForKey:key]; } - (void)setAge:(NSInteger)age { if (age >= 18) { [self willChangeValueForKey:@"age"]; _age = age; [self didChangeValueForKey:@"age"]; } else { _age = age; }}Copy the code
4. Does modifying attributes through KVC trigger KVO?
- KVO is triggered, even if there are no declared attributes, only member variables, as long as
accessInstanceVariablesDirectly
Returns YES, allowing access to its member variables, - Modifying the value of a member variable via KVC triggers KVO regardless of whether setter methods are called or not.
- This also indicates that it is implemented internally through KVC
willChangeValueForKey:
Methods anddidChangeValueForKey:
Methods.
5. Does directly modifying a member variable trigger KVO?
- KVO is not triggered. Direct changes to a member variable do no processing inside the variable, just pure assignments, so KVO is not triggered.
6. What is the underlying implementation of KVC?
- Assignment method
setValue:forKey:
The principle of
- The search is first done in order
setKey:
Methods and_setKey:
Method, as long as it finds either of these two methods, pass the argument directly and call the method; - If you don’t find it
setKey:
and_setKey:
Method, then it will be viewed at this timeaccessInstanceVariablesDirectly
Method, if NO is returned (that is, direct access to a member variable is not allowed)setValue:forUndefineKey:
Method and throws an exceptionNSUnknownKeyException
; - if
accessInstanceVariablesDirectly
Method returns YES, which means that its member variables are accessible, and are then looked up in order_key, _isKey, key, isKey
These four member variables, if found, are directly assigned; If still not found, then will callsetValue:forUndefineKey:
Method and throws an exceptionNSUnknownKeyException
.
- Accessor methods
valueForKey:
The principle of
- The search is first done in order
GetKey:, key, isKey, _key
These four methods are called as soon as any of these four methods are found; - If not, it will be checked at this point
accessInstanceVariablesDirectly
Method, if NO is returned (direct access to member variables is not allowed)valueforUndefineKey:
Method and throws an exceptionNSUnknownKeyException
; - if
accessInstanceVariablesDirectly
Method returns YES, which means that its member variables are accessible, and are then looked up in order_key, _isKey, key, isKey
These four member variables, if found, are evaluated directly; If the member variable is still not found, it is calledvalueforUndefineKey
Method and throws an exceptionNSUnknownKeyException
.
7. Use of KVC
- Values are dynamically evaluated and set
- Access and modify private variables
-
- Modify the internal properties of some controls
-
- Such as before
UITextField
theplaceHolderText
.
- Such as before
- Model and dictionary transformation