How can I set an object outside the scope of a block with an object inside the block’s scope?
My code is currently this, but it doesn’t work when I call NSLog(@"%d", [self publicFeed] count]) for example.
- (NSArray *) publicFeed {
__block NSArray *returns = nil;
[[APIRequester sharedRequester] getPublicFeedCompletion:^(NSArray *array) {
returns = array;
} fail:^(NSError *error) {
}];
return returns;
}
Here’s the problem. The blocks are called as you would expect, but this is what happens:
returnsas array.-[APIRequester getPublicFeedCompletion:fail:].returns(which isnil).~ ~ ~ ~ ~
returnsis set toarray.You have to change your code in some way that makes sure
returnsis set before the method returns.