reference

Ke.qq.com/course/3145…

KVO

KVO, commonly known as “key value listening”, can be used to listen for changes in the value of an object property.

The problem

1. Create a New Person class and set an age property.

2. Create two instances of Person and listen for the KVO property of the first Person instance.

The first Person isa is NSKVONotifying_Person, and the second Person ISA is Person.

4. Why was the ISA of the first Person object changed?

nature

KVO uses the Runtime API to dynamically generate a subclass and has the instance object’s ISA point to this new subclass. Foundation’s _NSSetXXXValueAndNotify function is called when modifying the properties of an instance object

1>willChangeValueForKey

2> Call the set of the parent class

3>didChangeValueForKey

DidChangeValueForKey internal will trigger the listener listening methods (observeValueForKeyPath: ofObject: change: context:)

supplement

1. How do I manually trigger KVO?

Manual calls to willChangeValueForKey: and didChangeValueForKey: trigger KVO.

2. Does directly modifying a member variable trigger KVO?

Assigning a value directly to a member variable without going through set does not trigger KVO.

3. If this article violates privacy or other things, please contact me, I will rectify or delete in the first time.