If you haven’t read my last post before reading this blog, you need to be patient to read it, which will help you understand the content of this blog faster and better

Ok, let’s continue with the last blog post: let’s solve this problem with constructs first

Take a look at the code below

Here we define a structure that stores three parameters. Let’s see if the result solves this problem: see the code screenshot below

So this obviously solves the case, you can try other cases, and then we’re going to introduce the most important module of the day, which is community union

Let’s first look at how we can use the common union to solve this requirement. I’ll write the result directly here, and I’ll discuss apple’s source code later. It also solves this problem, because we also know that bit calculation is very efficient, so look at the following figure:

This can be very good solve the problem, as for the result I won’t be the screenshot, you can have a try, certainly can solve this problem, in addition, my green part of the above can be annotated, did that is green is successful, that the green part is similar to an annotation to ourselves can see clearly.

Apple source code interpretation

Well, with the above foundation, I believe we are easy to understand apple’s source code, let’s go to have a look. Because its implementation is objc_Object and we can search for that and find it.

As we know, apple’s arm64 has made some improvements to isa. Previously, there was only one ISA pointer in the class. From what we learned earlier, an ISA is 8 bits, that is, 64 bits First look at the source code as follows:

Go in and look at the screenshot of the ISA_BITFIELD in the struct that the arrow points to

We’re going to look for arm64, and let’s see if the blue one is familiar, and it’s exactly the same way we defined it, and I’m going to write down what the green thing does, so you can see

We know that an ISA takes up 8 bits, or 64 bits, so let’s see how many bits it takes up: 1+1+1+33+6+1+1+1+1 +19=64

Now let’s test an earlier question, which is written in this blog to delve into the isa pointer to the object

Please see the screenshot below.

Previous blogs were written directly and did not prove why. Now to talk about it,

Why should I prove to get ISA &ISA_MASK

I want to know what nonpointer is in isa. I want to know if nonpointer is in ISA. I want to know if nonpointer is in ISA. Right?

So we had the ISA_MASK so you can see why, now let’s see what the mask is, we’re guessing it’s going to be 000 in the first three digits, 33 ones after that, because we’re taking out 33 bits, so let’s see if that’s right, so what’s ISA_MASK

Let’s use our calculator to see what base 2 is

This binary number corresponds to exactly 33 ones, so the & mask is exactly the address of the CLS of ISA, so you can see why the & mask is needed to fetch this value.

So let’s look at one that I said in my last blog, the last three digits of CLS are always 0

In isa memory addresses, the last three digits are always 0

In fact, the above proof, let’s take a look (remember to use the real machine to run the test is more rigorous, because it is ARM64), I don’t use the real machine, you can take a look

Well, I believe that here, the last blog four questions can be answered, for ISA I believe we have a deeper understanding than before, if the interviewer asked ISA I believe you can answer the essence!

expand

We actually have a lot of values running, like our enumeration values, we’re in iOS development, we’re passing any one or more of those enumeration values, how does Apple know which one we’re passing in? This is to use in place operations to deal with, specific we can write a demo test

In order to learn more about runtime, we need to learn more about the structure of the class. We also need to know what the internal structure of runtime is.

In the next part of this blog I will introduce the Runtime class structure to lay a foundation for learning more about Runtime

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