Friday, 22 November 2013

How to declare a variable which we need to use inside a block?

You must use the __block specifier when you modify a variable inside a block, so change your code to this:
 __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 declared inside, so we must declare that the variable is mutable. And mutability is exactly what you need considering that you're essentially setting this thing.

No comments:

Post a Comment