Menu

Category

Archive

logo


Block'c Modification and Scope

2013-09-21 05:03:00 +0900
  • このエントリーをはてなブックマークに追加

I often encounter Blocks when I write Objective-C. It's always kind confusing for me. The syntax is awkward and I honestly cannot still understand why using Block is useful. I have read really a helpful book about this topic and I found the different concept of scope in Blocks. I'd like to write down it here. I believe I will be confused about it again.

1. Modify Variable in Blocks

To modify variable, we should use "__block" when we declare the variable. Otherwise, our compiler display error if we try to modify variable.

 1 __block NSString *blala    = @"Use __block!!";
 2  NSString         *foolish  = @"Error will couse";
 3         
 4  void (^testBlocks)(void)   =
 5         ^(void){
 6             
 7             blala   = @"It's OK.";
 8             foolish = @"It's not allowed"; // Error
 9             
10         };

This code doesn't work because we are trying to modify foolish variable. If we comment the variable out, the code works. So, we should add __block at the declaration of this variable to change the value in Blocks.

2. Scope of Blocks

The scope of Blocks is more confusing. Let's take a look a simple example.

 1 NSString *blala = @"BLOCKS!!";
 2         
 3  void (^output)(void) =
 4         ^(void){
 5             
 6             NSLog(@"Blala : %@",blala);
 7             
 8         };
 9         
10         blala = @"Calm down.";
11         
12         output(); // Output : BLOCKS!

If we think blocks is a like function, we expect the out put which is "Calm down.". But actually this code will show "BLOCKS!!". The scope that blocks access is determined at the time blocks is called. Therefore, in this case ( 1. @"BLOCKS!!" 2. Blocks 3. @"Calm down"), this output is "BLOCKS!!"

I expected that calling this block to show "Calm down". To do so, we should use "__block" again like before.

 1 __block NSString *blala = @"BLOCKS!!";
 2         
 3 void (^output)(void) =
 4         ^(void){
 5             
 6             NSLog(@"Blala : %@",blala);
 7             
 8         };
 9         
10 blala = @"Calm down.";
11         
12 output(); // Output : Calm down

Just by adding __block, we changed the output.

I hope it was help to understand how block scope is determined and how to change a variable in blocks.