Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)" [duplicate] Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)" [duplicate] multithreading multithreading

Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)" [duplicate]


You must use the __block specifier when you modify a variable inside a block, so the code you gave should look like this instead:

 __block NSString *textString; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,                                                  (unsigned long)NULL), ^(void) {      textString = [self getTextString];});

Blocks capture the state of the variables referenced inside their bodies, so the captured variable must be declared mutable. And mutability is exactly what you need considering that you're essentially setting this thing.