meaning-matters
meaning-matters

Reputation: 22946

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

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:

Upvotes: 2

Views: 4443

Answers (1)

HalR
HalR

Reputation: 11073

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.

Upvotes: 11

Related Questions