Date
Jul. 14th, 2024
 
2024年 6月 9日

Post: __block directive, a simple explanation

__block directive, a simple explanation

Published 12:04 Apr 07, 2014.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

Nowadays, if you pass a variable inside a block and try to assign a new value to it, you will encounter the error Variable is not assignable (missing __block type specifier).

This is a definitive improvement from the previous behavior where your variables simply remained unmodified. Now consider the following example:

NSMutableString *digits = [NSMutableString stringWithString:@"12345"];

void (^someBlock) (void) = ^{
    [digits appendString:@"67890"];

};

someBlock();

NSLog(@"%@", digits);

Since digits was modified inside a block without the __block directive, you could think it will print the sequence "12345".

Surprisingly, digits actually holds the value "1234567890".

Why?

The answer is simple. When you make use of a variable inside a block. It is imported into the block structure as a const. Hence, the behavior is consistent with the declaration:

NSMutableString * const importedDigits = digits;

You would not be able to point importedDigits to a different address, but nothing stops you from changing the content of the object digits being referenced.

By using the __block directive, the variable is no longer imported to the block. Instead, it's passed by reference and its behavior will be consistent with the declaration:

NSMutableString **digitsReference = &digits;

In this case, you can move where digitsReference is pointing at and you will actually be affecting the content of the variable digits.

For more information on the topic, check out the Block Implementation Specification in Clang's documentation.

Pinned Message
HOTODOGO
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF