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

Why does Chang'e-5 adopt a "relay" method for sample return?

By December 2021, the Chang'e-5 sample return...

How do you write copy that will inspire users to place orders after reading it?

Copywriting with sufficient promotional power wil...

B Station UP host’s delivery direction and strategy!

UP hosts act as a medium connecting brands and us...

Redux Core Concepts

[[145606]] http://gaearon.github.io/redux/index.h...

Shadow video live streaming

Introduction to the live streaming resources of S...

Operators should use VoLTE to compete with WeChat Phonebook

Operators invest hundreds of billions of yuan eve...

What kind of fox is Lingna Belle? Could it be... a Tibetan fox?

If you were to ask who is the most popular idol t...

Only 3 tricks are needed to attract traffic and promote Xiaohongshu!

Xiaohongshu App is hailed as a magic tool for you...

Kuaishou short video traffic revenue project

Kuaishou short video traffic revenue project Kuai...

OLED dark room tear LCD TV: black beats the color of the real youren

The TV industry, which is used to being calm, has...