Since blocks are a little bit more involved, you might want to take a look at my last blog post before you read this to help you understand the three main types of blocks from source code, ARC, and MRC

Ok, so let’s move on

The underlying copy of a block,

In practice, we use __NSMallocBlock most of the time, because most of the time, we define the block there, wait until we want to use the call, so this kind of block is __NSMallocBlock, see the picture we know __NSMall OcBlock: __NSStackBlock calls copy. Ok, now let’s look at a question from a blog, see the picture below

Run in ARC environment

As we have seen before, this is __NSStackBlock, which is also __NSStackBlock under MRC. This blog was very clear, so why does the compiler print __NSMallocBlock? This is because the compiler encounters a situation where it automatically copies __NSStackBlock! Next, we will discuss the circumstances in which the compiler automatically copies __NSStackBlock to __NSMallocBlock in an ARC environment.

ARC: __NSStackBlock automatically converts to __NSMallocBlock

1. When a block is returned as a function (note that the block is still a stack block)

Take a look at the following code:

Run in ARC environment

MRC environment is very dangerous, you can directly switch to the MRC environment, run once to know.

2. Assign block to the __strong pointer

If you don’t know what a __strong pointer is, find out for yourself

Take a look at the following code:

Run in ARC environment

__NSMallocBlock__ is a stackblock with no strong pointer to it.

Run in ARC environment

That’s pretty clear

3. Block is used as a Cocoa API method name containing the usingBlock method parameter

Is the Foundation, apple’s own some methods, such as array traversal enumerateObjectsUsingBlock: this method is also introduced to block, so this is also __NSMallocBlock__ type

4. Block as a method parameter of the GCD API

The following

Run in ARC environment

Whenever the method argument in GCD is block, it is __NSMallocBlock__.

Ok, so that’s the case I’m talking about in the ARC case, which automatically converts __NSStackBlock to __NSMallocBlock__, if you have other views, or other cases, please discuss!

In the previous two blogs, the auto variable we discussed was a basic data type, such as int. However, in our practical work, we may encounter more object types. So in the next blog, I will introduce what happens when I encounter auto of object type?

In the next part of my blog, I’m going to talk about Block access auto as an object type and some of the specifics and uses of it to continue the discussion of blocks

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