tommi tommi - 1 year ago 200
Objective-C Question

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

I'm getting an error

Variable is not assignable (missing __block type specifier)

on the line
aPerson = participant;
. How can I make sure the block can access the
variable and the
variable can be returned?

Person *aPerson = nil;

[participants enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
Person *participant = (Person*)obj;

if ([participant.gender isEqualToString:@"M"]) {
aPerson = participant;
*stop = YES;

return aPerson;

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download