Block is a very common interview question on iOS. Such as:

1. How does Block work? What is the essence?

2. What is the function of _block? What is the use of attention?

3. Why does the modifier block copy? What are the implications of using blocks?

4. Block Is modifying NSMutableArray. Do I need to add _block?

Now let’s take you through the source code and understand the nature of the block. Let’s create a simple block as follows;

C ++ (c++); c++ (c++); c++ (c++);

Next, let’s pull the main. CPP file to the project and see how the source code actually implements the block. Open the CPP file, since main is executed last, so let’s go straight to the bottom of the file and see the following:

The above screenshot is the code we just wrote. We know that adding () to a variable is usually a cast, so we can remove the green box above and see the rest of the above. The top is relatively simple. These are __main_block_func_0 and _main_block_desc, and calling a block is directly –>funPtr is probably a pointer call,

Let’s take a closer look:

The 1 above is a C++ function. The inside, like the outside, is a constructor, and it returns the structure itself!

This is a very simple block call bottom! We know blocks aren’t that simple, so let’s look at a slightly more complicated one

Easy upgrade: Add the following parameters

Then convert the file into a c++ file using the same method as above

This one is also very clear and relatively easy, and we’re going to upgrade it a little bit

Easy upgrade: set a variable, access inside, and then modify the variable, the code is as follows

In this case, before we publish the result, some of you may think that the result is 30, some of you may know that the result is 10, but how does 10 come from, let’s continue to explore, or should we generate c++ code to see

Figure 1

Height =30 (height= 10); height=30 (height= 10); It’s pretty obvious here, so we call a block and it says Hello, World:10, and it’s pretty obvious, and that relates to what we sometimes interview people say, you know what a block does to capture variables.

Block variable capture mechanism

Let’s start with a picture

In our example above, we’re talking about local variables, and we’re actually saying int height = 10; Auto int height = 10 auto int height = 10 auto int height = 10 auto int height = 10

Block capture: a new variable is added to the block to store the value of the variable. This is called capture.

The static case is shown below:

Static is the result of a pointer pass, and the result is 30. Or look at the source code interpretation

In addition to the source code, we can conclude that both auto and static local variables can be caught by the block. Age = &age; age = &age; age = &age;

Get the address too! This is very clear, why can appear above result!

Consider: why can’t the auto variable also set the transfer address?

You may have this question, let me write an example to illustrate the situation:

The picture above illustrates the problem.

Global variables are not captured directly, which you can prove yourself, according to my method above, should be very clear

At this point, I believe that we should have a relatively essential understanding of block variable capture!

You only need to know whether this variable is local or global to know whether it can be captured!

Think about:

I’m going to leave you with a question about whether the block catches either of the following:

Will test and test1 variables be captured?

I will introduce the types of blocks in the next blog post. Let’s continue with block

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