1. Implementation principle of KVO

  • throughruntimeSubclasses 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 classClass, such asNSKVONotifying_Person
  • Isa pointer structure diagram for objects not listened on by KVO

  • Isa pointer structure diagram for objects that use KVO listening

  • _NSSetValueAndNotifyInternal implementation of

  • An internal method of a subclass
  • classMethod hides the internal implementationNSKVONotifying_MJPersonClasses 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:contextWill 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: cancelPersonclassageProperty. 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 asaccessInstanceVariablesDirectlyReturns 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 KVCwillChangeValueForKey: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 methodsetValue:forKey:The principle of
  1. The search is first done in ordersetKey:Methods and_setKey:Method, as long as it finds either of these two methods, pass the argument directly and call the method;
  2. If you don’t find itsetKey:and_setKey:Method, then it will be viewed at this timeaccessInstanceVariablesDirectlyMethod, if NO is returned (that is, direct access to a member variable is not allowed)setValue:forUndefineKey:Method and throws an exceptionNSUnknownKeyException;
  3. ifaccessInstanceVariablesDirectlyMethod returns YES, which means that its member variables are accessible, and are then looked up in order_key, _isKey, key, isKeyThese four member variables, if found, are directly assigned; If still not found, then will callsetValue:forUndefineKey:Method and throws an exceptionNSUnknownKeyException.

  • Accessor methodsvalueForKey:The principle of
  1. The search is first done in orderGetKey:, key, isKey, _keyThese four methods are called as soon as any of these four methods are found;
  2. If not, it will be checked at this pointaccessInstanceVariablesDirectlyMethod, if NO is returned (direct access to member variables is not allowed)valueforUndefineKey:Method and throws an exceptionNSUnknownKeyException;
  3. ifaccessInstanceVariablesDirectlyMethod returns YES, which means that its member variables are accessible, and are then looked up in order_key, _isKey, key, isKeyThese four member variables, if found, are evaluated directly; If the member variable is still not found, it is calledvalueforUndefineKeyMethod 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 beforeUITextFieldtheplaceHolderText.
  • Model and dictionary transformation