Date
Sep. 8th, 2024
 
2024年 8月 6日

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
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系