The KVO Observer pattern realized in OC is rather stiff, does not support block, and realizes it by using runtime mechanism.
Detailed implementation steps:
One: Add a category to the NSObject (add the appropriate observer, delete the Observer's method), so that all inherited NSObject class generated objects will have these two methods.
Second: In the method of adding observers, check whether this class has attributes that the observer wants to observe. If so, proceed to the next step.
Third: Check whether the set method of this property of the object has been replaced by the method implementation, if there is no next step, if there are fifth steps.
Four: The implementation of this property's set method points to the AOP method, and adds a corresponding method to the implementation of the original set method.
Five: The Observer (which can be an object that encapsulates the properties of an observer, I just encapsulated an additional class) into my own dictionary (I use a dictionary or other model).
Six: Write the AOP method, the AOP method implements the call to the original set method, and implements the broadcast to the Observer. (The call order of two methods can be customized, which is AOP pre-and post-cut)
Tips:
The above is just a simple process step, message to provide source code, welcome to discuss the study together.
Using runtime and AOP to realize KVO in OC