1
votes

I have a method that returns a value (an NSArray), and the implementation of the method contains calls a method that has a completion block. What I would like to do is to return an NSArray that is obtained from within the completion block.

Is there any way that I can delay returning a value from this method until the completion block has finished executing? Thank you.

1
Can you provide a code sample?trojanfoe

1 Answers

0
votes

So, you mean you want something like this:

- (NSArray*)someMethod {
    [self someMethodWithACompletionBlock:^(NSArray *array) {
        return array;
    }];
}

Well, unfortunately, it ain't gonna happen like that (because the block is a function, returning inside of it makes the compiler think you're trying to return a value from the block rather than the enclosing method), but you can make the array an out-parameter and use a blocking function to return a properly-mutated array:

- (NSArray*)someMethod {
   NSArray *retVal = nil;
    [self someMethodWithAnOutParameter:&retVal];
    return retVal;
}