Internal implementation of KVC

KVC uses ISa-Swizzing technology. Isa-swizzing is the type mixing pointer mechanism. KVC realizes its internal search and positioning through ISa-Swizzing.

The ISA pointer (is kind of) points to the class that maintains the object of the subtable, which actually contains Pointers and other data to the methods in the implementation class.

& emsp; [site setValue:@”sitename” forKey:@”name”] SEL = sel_get_uid(setValue:forKey); IMP method = objc_msg_loopup(site->isa,sel); method(site,sel,@”sitename”,@”name”);  

  • Each class has a method table, which is a hash table. The value is the return pointer IMP, and the name SEL is the key used to look up the table.
  • SEL data type: the key used to find the method table. Char *, which is essentially an int.
  • IMP data type: it is actually a compiler internal implementation of the function pointer. When the Objective-C compiler handles implementing a method, it points to an IMP object, which is a type of C representation.

Internal mechanics of KVC:

An object does the following when calling setValue:

  • Find the environment parameters needed to run the method based on the method name
  • He will find specific ways to implement the interface from his OWN ISA Pointers combined with the environment parameters.
  • Then directly find the specific implementation method

KVO overview

Key-value Observer key-value Observer is the Observer mode.

  • Definition of observer pattern: A target object manages all observer objects that depend on it and actively notifies them of changes in its own state. This active notification is usually implemented by calling the interface methods provided by each observer object. The observer pattern perfectly decouples the target object from the observer object. KVO can be used when you need to detect changes in attribute values of other classes, but you don’t want the class being observed to know about them, a bit like the FBI spying on suspects.

KVO, like KVC, relies on the dynamic mechanism of Runtime

  • Registration:  -(void)addobserver:(nsobject*)anobserer forkeypath:(nsstrig*)keypath options:(nskeyValueObservingOptions)options context:(void*)context{}  
  • Implementation method:- (void) observerValueForKeyPath:(nsstrig*)keypath ofOject:(id)object change:(nsdictionary*)change context:(void*)context
  • Removed:- (void) removeObsever: (nsobject) observer forkeypath (keypath nsnsting *);

KVO implementation steps

Register //keyPath is the property value to view //options give you options to view key changes //context to transfer data you need -(void)addObserver:(NSObject *)anObserverforKeyPath: (nsstrings *) KeyPath options: (NSKeyValueObservingOptions) options context: (void *) method to realize the context // Change store some data, such as before the change of data, after the change of data; If the context isn't empty when you register it, then the context will receive it. -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context remove - (void)removeObserver:(NSObject *)observerforKeyPath:(NSString *)keyPath;
Copy the code

The system has the following steps to achieve KVO:

  • When objects of class A are first observed, A derived class of class A is dynamically created at run time. Let’s call that B.
  • The setter methods of class A are overridden in derived class B, which implements the notification mechanism in the setter methods being overridden. Class B overrides the class method, disguising itself as class A. Class B also overrides the dealloc method to free resources.
  • The system points all isa Pointers to objects of class A to objects of class B.
  • Like KVC, KVO is implemented by ISa-Swizzling technology. When the observer is registered as a property of an object, the isa pointer to the observed object is modified to point to an intermediate class rather than the real class. As a result, the value of the ISA pointer does not necessarily reflect the actual class of the instance.

So you cannot rely on isa Pointers to determine whether an object isa member of a class. The class method should be used to determine the class of an object instance.