First let’s see what the interview question is.

It looks pretty simple. I created a GDPerson class that inherits NSObject;

The.h file of the GDPerson class

The.m file for the GDPerson class

Take a look at our viewController.m code again:

This is the title

Excuse me:

1. Can print be called successfully? What happens if you can’t? If so, what is the result of the call?

This is a more pull the interview question, the real development of the time, who will not write so, this is still the main test your foundation! Believe you to see this topic should have the answer in the heart, do not know the result, or may know the result, or hesitating, or do not know ha ha!

In fact, there are many questions for the interview.

1. You need to understand the nature of super. The first argument is passed to the structure

2. Stack allocation of functions

3. Message mechanism, how to call the method

4. Access the essence of a member variable

So, let’s look at the result of the call first!

Look at the results:

Apply the results of the interview questions

Is this the answer you think it is?

So I’ll add some code in front of CLS, and let’s see what happens:

Apply the results of the interview questions

First of all, we immediately have two questions:

1. Why is the call successful?

2. Why does self.name result in viewController?

I. Why can call successfully?

[(__bridge id)obj print]; Print (obj) : print (obJ) : print (obJ) : print (obJ) : print (obJ) : print (obJ) : print (obJ) : print (obJ) : print (obJ) : print (obJ) : print (obJ) This can be called successfully, indicating that (__bridge ID)obj also exists

Let me draw a picture just to make it clear.

CLS points to GDPerson and obj points to CLS, so the figure is as follows:

The green color above is [GDPerson Class], which is fuzzy

Take a look at the following code:

It is also clear that person points to instance variables of GDPerson, which include ISA and member variables, etc. Isa isa class object that points to GDPerson, so look at the following diagram:

​​

As we know from previous source code analysis,isa and _name exist in a structure and the address value of the first member variable is the address of the structure. So person refers to ISA.

Ok, after we have analyzed these two graphs clearly, you can see whether these two graphs are very similar, almost the same, let’s look at the following graph:

The green color above is [GDPerson Class], which is fuzzy

So it’s the same thing from the structure above,obj is the same thing as person, so you can call it, and this is a little bit abstract. In plain English, CLS is what isa does, because we know that the pointer to a class object is isa. Isa is the address value of a class object. How can a GDPerson object be a CLS without a _name in it, you might wonder? Note that we are calling the print method and the calling method does not say there must be a _name member variable! Is that right? It doesn’t matter whether there’s a member variable or not.

The first eight bytes of the CLS object are also the address of the structure. The first eight bytes of the CLS object are the address of the structure. It is easy to find the class object by the address. So it can call successfully! That’s the nature of the call, and so is the call to the next line.

Why does self.name result in something else?

The first thing you need to know about stack arrangement is the following:

These variables are stored in stack space, and the memory address is from high address to bottom address.

Ok, let’s look at the previous picture below:

So let’s draw these addresses as follows

Structure diagram of the code above

The top green is [GDPerson Class], the graph is fuzzy, the graph is very clear.

Now let’s recall that when we defined an object, such as the GDPerson class above, its underlying structure would look something like this

structGDPerson_IMPL

{

Class isa;

NSString *_name;

}

Select * from isa where id = 1; select * from isa where id = 1; select * from ISA where id = 1; See below

Name = ‘name’; isa = ‘name’; isa = ‘name’;

So now you know what happened? CLS = test; CLS = test; So let’s run it again

Here you define test, and you define anything else, and you find the variable that CLS declared before. Let’s say I define an objct and let’s see.

The output is the last one created above the CLS. One of the things that’s still unresolved is that the self.name call results in a viewController

Self. name results in a viewController.

So if we get rid of the test variable, it’s going to be the viewController and I’m just saying that because it’s [super viewDidLoad]; We know that from the last blog

What super does is it implements it in the bottom line like this (it was pretty clear in the last blog): objc_msgSendSuper({self,[UIViewController Class]},@selector(viewDidLoad)); The other thing is if you do that **@selector**(viewDidLoad) you can also say sel_registerName(“viewDidLoad”)

This must start defining a local structure to pass in the objc_msgSendSuper method. So the highest address is the ABC structure, and the address of the first argument to the structure is the address of the structure, so the output is self which is the viewController.

The diagram below:

I’m going to find self

Let’s prove this in memory:

This topic involves knowledge points or more, if you directly give the topic to think about, or it is difficult to come up with the answer, well, so much to say

I’m going to continue learning about the Runtime by introducing it in action.

If you find my writing helpful, please follow me and I will continue to update 😄