user4951
user4951

Reputation: 33138

What exactly does __block do?

Yes we do that if the block may change the variable.

But what's really happen behind the screen?

Who "own" the object? The block or the function, or who?

How does __block make that different?

Here is some sample:

__block NSError * error=nil;
__block NSURLResponse *urlresponse=nil;
__block NSData *response = nil;
NSString *json_string=nil;

[BGHPTools computeTimeWithName:FUNC block:^{
    response= [NSURLConnection sendSynchronousRequest:request returningResponse:&urlresponse error:&error];

}];

Upvotes: 1

Views: 234

Answers (1)

jithinroy
jithinroy

Reputation: 1885

From Appl doc

__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.

Upvotes: 5

Related Questions