Let’s see this example:
int a = 1;
[someOperation onCompletion:^(...) {
NSLog("%d", a);
}];
a = 2;
Let’s say someOperation took 1 second to finish.
So there would be “2” on console.
How to define callback body with variable values from the time of defining the callback (so that in this case callback execution would print “1” on console) ?
In JavaScript it could be done as follows:
setTimeout("someFunction("+variable+")", 1000);
instead of
setTimeout(function() { someFunction(variable); }, 1000);
But how to accomplish this thing in ObjC ?
Your assumption is not correct.
A block copies local variables unless they are declared with the __block keyword.
So, in your case:
NSLog statement would return 1.
If you change it like this:
it would return 2, because in this case a is accessed by reference