There is a topic in the iOS question collection (attached answer):
How do I modify the block external variables within a block? (38 questions) The answers are as follows:
By default, external variables that are accessed in the block are copied in the past, that is, the write operation does not take effect on the original variable. But you can add __block to make the write operation work, the sample code is as follows:
__block int a = 0;
void (^foo) (void) = ^{
a = 1;
};
foo();
//Here, the value of a is modified to 1
This is the description in the 11th. 2.3 section of "iOS development advanced" at Weibo @ Tang Qi _boy. You can also answer this question in the interview, but you don't answer the "Pip". The real reason is that there is no such thing as "Magic" written in the book, and it is a bit farfetched. The interviewer will surely ask, "Why does the writing work?" "The real reason is this:
As we all know: block does not allow to modify the value of an external variable, the value of the external variable referred to here refers to the memory address of the pointer in the stack. The function of __block is to place the memory address of the "external variable" on the stack as long as it is observed that the variable is held by block. The value of the external variable can also be modified inside the block.
Block does not allow to modify the value of an external variable apple is designed to take into account the particular nature of the block, which is also the category of "functions", the variable into the block, the actual change of scope. When you switch between several scopes, the maintainability of the variable is greatly reduced if you do not add such a restriction. Another example is if I want to declare a variable with the same name inside the block, is this allowed or not? This can only be achieved with the addition of such a limit.
We can print out the memory address to verify:
__block int a = 0;
NSLog(@"pre-defined:%p", &a); //Stack area
void (^foo) (void) = ^{
a = 1;
NSLog(@"block internal:%p", &a); //Heap area
};
NSLog(@"after definition:%p", &a); //Heap area
foo();
To: 33.559 leancloudchatkit- IOS[1505:713679] before definition:0x16fda86f8
To: 33.559 leancloudchatkit-IOS[ 1505:713679] after definition:0x155b22fc8
To: 33.559 leancloudchatkit- IOS[1505:713679] block internal: 0x155b22fc8
"After definition" and "block internal" memory address is the same, we know that the variables inside the block will be copied to the heap area, "block internal" printing is the heap address, so you can know, "after the definition" is also printed heap address.
So how do you prove that "block interior" prints a heap address?
Turning three 16-byte memory addresses into 10 binary is:
-
Pre-defined: 6171559672
-
Block internal: 5732708296
-
After definition: 5732708296
The difference between 438,851,376 bytes, that is, 418.5M of space, because the heap address is less than the stack address, and because iOS in a process of the stack area of memory only 1m,mac also only 8M, obviously a is already in the heap area.
This also confirms that a is a stack area before the definition, but as soon as it enters the block area, it becomes a heap area. This is the true role of the __block keyword.
It is important to understand that this is because of the change in the stack address, not the so-called "write operation", or how you interpret the following behavior:
The following code compiles can be passed and successfully modifies A's from Tom to Jerry in block.
nsmutablestring *a = [nsmutablestring stringwithstring:@"Tom"];
NSLog(@"\ n" before:------------------------------------\n\
A point in the heap address:%p;a the pointer in the stack address:%p ", a, &a); //a in the stack area
void (^foo) (void) = ^{
a. String = @"Jerry";
NSLog(@"\ n Block internal:------------------------------------\n\
A point in the heap address:%p;a the pointer in the stack address:%p ", a, &a); //a in the stack area
a = [nsmutablestring stringwithstring:@"William"];
};
foo();
NSLog(@"\ n:------------------------------------\n\
A point in the heap address:%p;a the pointer in the stack address:%p ", a, &a);
Here A has been changed from the basic data type to the object type. Object type, block copy,copy a pointer to the object type into the heap, but does not change the address in the heap to which the pointer points, so in the example code above, the block is actually modifying the contents of the heap that a points to.
But if we try to do it like 65 lines in the picture above, the result will not compile, because at this point you are not modifying the contents of the heap, but the contents of the stack.
As mentioned above, block does not allow the value of an external variable to be modified, and the value of the external variable mentioned here refers to the memory address of the pointer in the stack.
The underlying implementation of the __block keyword in ios