Runtime series (analysis of data structure)

Runtime series (analysis of data structure)

In the previous article, we talked about classes and metaclasses. We already know that the essence of a class is the structure objc_class. Next, let's see what objc_class is.

objc_class.png

isa points to the metaclass, and super_class represents the parent class of the current class. We are already familiar with these two members and will not go into details here (see Class and Metaclass, Reference).

name: class name
version: Version related information, the default value is 0
info: Provides identifiers used during runtime
instance_size: The size of the instance variables of the current class (including the parent class)

  • ivars
    As can be seen from objc_class, ivars is a pointer to the structure objc_ivar_list

objc_ivar_list and objc_ivar.png

The names of the members of the structure are self-explanatory, so we will not explain them one by one. As you can see, ivars is actually a linked list that stores information related to member variables in a class.
in

Ivar.png
  • methodLists
    As can be seen from objc_class, methodLists is a secondary pointer to the structure objc_method_list

objc_method_liist and objc_method.png

See the self-nesting of the structure again, and you can see that methodLists is also a linked list, storing information about the methods in the class. Because it is a secondary pointer, the methods in the class can be modified dynamically, which is also the implementation principle of classification.
in

Method.png

Here we need to explain SEL and IMP:

  • SEL
    • What is SEL
      SEL is a wrapper for methods. Common definitions include
      SEL sel1 = @selector(message1);
      SEL sel2 = NSSelectorFromString(message2);
    • Why do we need to package the method to obtain the ID corresponding to the method?
    • What is the ID corresponding to the method?
      It can be understood as a mapping of method names

Let’s look at the following example.

- (void)helloWorld:(int)flag;
- (void)helloWorld:(float)flag;

In OC, this will result in an error message, and the error type is duplicate declaration. If you write:

- (int)helloWorld:(int)flag;
- (float)helloWorld:(float)flag;

Even though the return values ​​are different, they are still repeated declarations. Because their method names are the same, helloWorld:, these four methods correspond to the same SEL.
But this is in the same class, what if it is a different class?
Whether in the same class or in different classes, as long as the method name is the same, the SEL is the same and the obtained ID is the same.

Since the method names are the same, the IDs are the same. If two classes that are not in an inheritance relationship have methods with the same method name, how do we determine which class’s method to execute?
Let's review the function mentioned in the quotation

id objc_msgSend(id self, SEL op, ...)

[receiver message] There is also a receiver. Even if the ID is the same, different receivers will locate different methods, and methods with the same method name are not allowed to exist in each category, thus ensuring security.

  • IMP
    Compared with SEL, IMP is much more straightforward. The essence of IMP is function pointer, and each method can be found directly through IMP. This is more efficient because it bypasses the message passing stage and directly locates.

Back to objc_class.
We will not go into detail about cache and protocols, but will only give a brief introduction here.

  • cache
    The cache is also a linked list that stores information about methods that have been called. By storing commonly used methods in the cache, the efficiency of method search can be improved.
  • protocols
    Protocols is still a linked list, storing relevant information about the protocols followed by the current class (including the parent class).

<<:  Is the decline inevitable? With less than 1% market share, what happened to Microsoft WP?

>>:  Real-time display of iOS UI code writing effect

Recommend

How long will it take for your job to be replaced by a machine?

Many people believe that the world is about to us...

3 new customer acquisition cases, 3 key logics for attracting new customers!

There are many ways to attract new customers , bu...

H5 fission communication skills that can sweep the screen!

In recent years, H5 has gone from being easy to f...

Relearn data structures and algorithms

Relearning Data Structure and Algorithm Resources...

Shanghai Auto Show opens tomorrow with 113 new cars making their global debut

The 17th Shanghai International Auto Show will op...