Block pointer variable 'block' is uninitialized when captured by block

grand-central-dispatch, ios, objective-c

Solution

To just declare your block you would use

void (^block)(void); 

then initialize it with

block =^  // Get warning here!
{
    next = dispatch_time(next, DELAY_IN_MS * 1000000L);

    // Do my periodic thing ...

    dispatch_after(next, dispatch_get_main_queue(), block);
}

That is why putting in the semicolon works.

Why it gives you an error without the semicolon: you are referencing block in its own declaration/assignment. You are using it in the "dispatch_after" call, but its not fully set up yet.

Problem

I'm using the code seen here to execute code periodically: ``` #define DELAY_IN_MS 1000 __block dispatch_time_t next = dispatch_time(DISPATCH_TIME_NOW, 0); void (^block)(void) = ^ // Get warning here! { next = dispatch_time(next, DELAY_IN_MS * 1000000L); // Do my periodic thing ... dispatch_after(next, dispatch_get_main_queue(), block); } ``` This results in a warning (see title). I have two questions about this warning: - What does it mean? - Why does the warning go away when I write: `void (^block)(void); block = ^`?

Original source

Related problems