Objective-C blocks and variables

11,531

Solution 1

It's because the block captures variables by value and when the block is created (unless you use __block).

What you probably want is:

NSArray *array = @[@25, @"abc", @7.2];

void (^print)(NSUInteger index) = ^(NSUInteger index)
{
    NSLog(@"%@", array[index]);
};

for (int n = 0; n < 3; n++)
    print(n);

Example with __block:

__block NSArray *array;

void (^print)(NSUInteger index) = ^(NSUInteger index)
{
    NSLog(@"%@", array[index]);
};

array = @[@25, @"abc", @7.2];

for (int n = 0; n < 3; n++)
    print(n);

Note that it's a little less efficient to use __block if you don't actually need to modify the variable inside the block and have it reflected outside.

Solution 2

The block captures the array pointer at creation. You can add __block modifier to have the block capture the pointer by reference, but this is usually costly and not recommended. It is better to have the capturing block created after the data is ready to use inside the block.

Share:
11,531
Tim Vermeulen
Author by

Tim Vermeulen

Swift and Rust programmer.

Updated on July 28, 2022

Comments

  • Tim Vermeulen
    Tim Vermeulen almost 2 years

    I've started using Objective-C blocks today. I wrote the following code:

    NSArray *array = @[@25, @"abc", @7.2];
    
    void (^print)(NSUInteger index) = ^(NSUInteger index)
    {
        NSLog(@"%@", array[index]);
    };
    
    for (int n = 0; n < 3; n++)
        print(n);
    

    Which works properly. I needed to change the array variable after its declaration, though, so I tried using the following code:

    NSArray *array;
    
    void (^print)(NSUInteger index) = ^(NSUInteger index)
    {
        NSLog(@"%@", array[index]);
    };
    
    array = @[@25, @"abc", @7.2];
    
    for (int n = 0; n < 3; n++)
        print(n);
    

    However, that doesn't work. The console just prints (null) three times. Why is it that this doesn't work, while it did work with my first piece of code?

  • pro_metedor
    pro_metedor over 11 years
    What is the difference while using __block ?
  • Chris Hillery
    Chris Hillery over 11 years
    __block tells the compiler to capture the variable by reference, so you can modify it inside the block and have the new value visible outside the block. (I'll add an example with __block.)
  • Léo Natan
    Léo Natan over 11 years
    __block captures variables by reference. Edit: Beaten on both occasions. :)
  • Tim Vermeulen
    Tim Vermeulen over 11 years
    Thanks, using __block did it for me. I actually needed to modify my variable inside my block, which works properly now. :)
  • GodFather
    GodFather almost 11 years
    interesting did you have an example?
  • ikel
    ikel almost 5 years
    i tried with modifying variable inside block with this code, but print three nulls, any idea? __block NSMutableArray *array; void (^print)(NSUInteger index) = ^(NSUInteger index) { [array addObject:@"444"]; NSLog(@"%@", array[index]); }; [array addObject:@"111"]; [array addObject:@"222"]; [array addObject:@"333"]; for (int n = 0; n < 3; n++) print(n);