What is it like to create a serial-oc object

Discussion on OC method call

In the last article we learned about the OC object memory structure, so how does the method call succeed? Let’s talk a little bit about it. We won’t go too far into runtime, but we’ll talk more about runtime and messaging.

  • As you saw in the last brief introduction, object methods are stored in class objects, and class methods are stored in meta-classes

Object methods

Instance objects call object methods
1. Use the ISA pointer -> find the class object to which you belong -> find and call the method
2. If there is no method found in your own class object, use the superclass pointer of the class object -> find the superclass object of the parent class -> find and call the method
3. If no superclass object is found, use the superclass pointer to find the base class object. If no superclass object is found, throw an exception

Class method

Class objects (classes) call class methods
1. Class objects find their metaclass objects via isa Pointers -> find and call methods
2. If not, use the superclass pointer to the metaclass object -> find the parent metaclass object -> find and call the method
3. If still not found, according to the inheritance system, through the superclass pointer to the metaclass object to find the base class metaclass object, search and call the method
4. If the metaclass object of the base class also cannot find the method of that class, it willFind the class object of the base class through the superclass pointer of the base class metaclass object, find if there is an object method with the same name, and call itIf no, throw an exception

Note:

Whether it is a class method or an object method, the principle of finding a method is to first find this class (or metaclass), and then through the inheritance relationship to find (for example, call a parent class method, is sure to first find in their metaclass object, and then look up. The system will never know in advance that you are calling a method of the parent class.)

Although it is possible for a class to call an object method with the same name as its base class when it cannot find a corresponding class method (via the superclass pointer), an object will not look for a metaclass object with the same name even if it cannot find an object method. (I don’t know what Apple is thinking, if anyone knows, please let me know, thanks).