SOURCE http://www.wtoutiao.com/p/11dgbk4.html
Let's take a brief look at block.
What is block?
Apple's recommended type, high efficiency, saves code in the run. Used to encapsulate and save code, a bit like a function, block can be executed at any time.
Block and Function Similarity: (1) can save Code (2) has a return value (3) The physical parameter (4) is called the same way.
Block Bottom Implementation
Define a simple block
Let's assign a to a value of 20, and then print out the value of a or 10.
But when we assign a value to a for the first time, when we add __block to the front, we print out 20.
So why add __block and then print out 20, what is the principle?
In fact, it can be summed up in two words: the value of the transmission and the address. This might be a bit of a rip-off, and then compiled with C + + code.
Open the terminal and do the following to get a. cpp file under the current folder.
Now open the current. cpp file (there will be almost 100,000 lines of code), before we all ignore, only need to scroll to the end, you will find the block and OC changes.
Next we look at this block, first look at the equal sign to the left.
void(*block)()
This is a function pointer without parameters and no return value, since it is a function pointer, it is a variable, the variable can only save the function address, and then it is on the left side of the equal sign means that the right is returned to a function address (self inference).
Look at the right side of the equals sign:
((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, a));
((Void (*) ()) Strong turn (own understanding actually has no actual meaning, does not affect own type)
& is followed by a function call, and if not, a function pointer is not obtained.
__main_block_impl_0 This is a function name, this function has three parameters, Com+f search, and will find that this is a structure, the structure is as follows:
struct __main_block_impl_0 { struct __block_impl impl; struct __main_block_desc_0* Desc; int a;
You might be wondering, just saying this is a function, and now it's a struct. In fact, in C + + structure equivalent to the OC class, C + + structure has its own properties and construction methods and methods. So why take the address of a struct? Actually it gets the address of the following code:
__main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _a, int flags=0) : a(_a) { impl.isa = &_NSConcreteStackBlock; impl.Flags = flags; impl.FuncPtr = fp; Desc = desc; }
Then in the above method implementation, there are four parameters. And in just called when only three parameters, more than one parameter flags= 0, this parameter is actually equivalent to the swift specified a default value, does not pass also has the value, can ignore. Then continue back:
A (_a): in C + + specify _a (formal parameters) will be assigned to a this argument, that is, the __MAIN_BLOCK_IMPL_0 structure of the int A; here int a = 10;
Impl. funcptr = FP; The FP is assigned to the FUNCPTR parameter of the Impl struct, where the address of the following code is stored in this parameter:
static void __main_block_func_0(struct __main_block_impl_0 *__cself) { int a = __cself->a; // 这里 int a = 10; printf("%d\\\\\\\\n",a); // 打印出a } __main_block_desc_0_DATA com+ F 搜索 定义的就是与大小相关的信息,代码如下: static struct __main_block_desc_0 { size_t reserved; size_t Block_size; } __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0)};
A directly put a actually is equivalent to the current value of a to take over, if it is &a, is the address of a. Please see:
Next, the A is assigned a value of 20, but the block will eventually find the funcptr inside the value to execute, here will finally execute the code to print the value of a, but in this code, a is 10. So the final print is still 10.
Finally, it can be summed up as block bottom implementation of two kinds: just above is the first (without __block), will create a struct, implement the construction method, to receive three parameters.
Next look add __block implementation.
To modify our code:
Once again in the terminal to compile, you will find the structure of the build experience changes.
The left side of the equals sign encapsulates a variable a of the __block_byref_a_0 struct type, and the following is the declaration of the struct:
truct __Block_byref_a_0 { void *__isa; //isa 类型的指针 自己的类型 __Block_byref_a_0 *__forwarding; //与自己结构体同名,是一个自己类型的结构体的指针,存放的是自己的地址 int __flags; // 标记 int __size; // 类型的大小 int a; // a 属性 保存变量的值 };
Right side of equal sign:
{(void*)0,(__Block_byref_a_0 *)&a, 0, sizeof(__Block_byref_a_0), 10};
Here is just create, because the use of __block so create a block type of structure, next will be called block, you will find that the rest of the parameters and the first implementation are the same, the only difference is to go to the value of the time, get the structure of the address, as long as the address passed, You have the highest operating privileges, and then you can fetch the value to get the latest value in memory.
Next (a.__forwarding->a) = 20; This code is to get the address inside the structure to modify the value of a to 20.
Back to print, printing is the memory address of the latest value, so it is 20.
Liwjing地址:http://www.jianshu.com/users/8df89a9d8380/latest_articles
IOS OC Language: block Bottom implementation principle