Obj ->isa can be obtained using object_getClass(obj)
To change the ISA pointer to obj, use the object_setClass(id obj, Class CLS) method
Set Build Settings -> Enable Strict Checking of objc_msgSend Calls to NO **
How does KVO access attribute values
- Check for accessor methods -, -is (valid only for Booleans), or -get, and return values with these methods if possible; Check for a method named -set: and use it for the setting value. For the -get and -set: methods, uppercase the first letter of the Key string and keep the method names consistent with Cocoa;
- If the above methods are not available, check for methods named -_, -_is (valid only for Booleans), -_get, and -_set:;
- If no accessor method is found, try accessing the instance variable directly (
ivar
). Instance variables can be named: or _; - If you still to find, then the call valueForUndefinedKey: and setValue: forUndefinedKey: method. The default implementation of these methods is to throw exceptions, and we can override them as needed.
KVO implementation principle
When observing object A, the KVO mechanism dynamically creates A subclass of object A’s current class (NSKVONotifying_A). NSKVONotifying_A overrides the following methods.
- A setter method. Override setter methods and override the observed property keyPath setter methods for this new subclass. Setter methods are then responsible for notifying the observed object of changes to its properties.
- Class method, the implementation returns the parent class A, similarly
- (Class) class {return class_getSuperclass(object_getClass(self)); }
- Dealloc method
- The _isKVOA method, which identifies whether the class is dynamically generated by the KVO runtime.
More importantly, KVO modifies the ISA pointer to the monitored object.
The resources
- Detail key value observation (KVO) and its implementation mechanism
- Summary of basic principles of iOS – Explore the essence of KVO