Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C Blocks, Recursion Fails

Tags:

Sup guys,

I'm trying to do a function that calls itself but by putting everything on one block,

As you can see, the following function is intended to be called an indefinite amount of times (until arcrandom returns a number lower than 50) and you should expect as an output a variable number of "RUNNING" messages, depending on chance.

void (^_test_closure)(void) = ^ {
    NSLog(@"RUNNING");
    if(arc4random() % 100 > 50) {
        _test_closure();
    }
};

_test_closure();

However, when running it, I get an EXC_BAD_ACCESS error and the reason I've found is that when the code tries to calls _test_closure inside of the closure it basically points to nowhere.

Does anyone know how to make the above code work?