Assign a variable inside a Block to a variable outside a Block Assign a variable inside a Block to a variable outside a Block objective-c objective-c

Assign a variable inside a Block to a variable outside a Block


You need to use this line of code to resolve your problem:

__block Person *aPerson = nil;

For more details, please refer to this tutorial: Blocks and Variables


Just a reminder of a mistake I made myself too, the

 __block

declaration must be done when first declaring the variable, that is, OUTSIDE of the block, not inside of it. This should resolve problems mentioned in the comments about the variable not retaining its value outside of the block.


Just use the __block prefix to declare and assign any type of variable inside a block.

For example:

__block Person *aPerson = nil;__block NSString *name = nil;