I’ve writen a class to parse XML on block. The completionHandler return an mutableArray and an error (if it happens), the problem is that I can NSLog the element of the array but if I init a __block NMutableArray with the array it return null:
__block NSMutableArray *imagesURLs;
NCBlockParser *parser = [[NCBlockParser alloc] init];
[parser parseXMLFromURL:url withElementsName:[NSArray arrayWithObject:@"element"] completionHandler:^(NSMutableArray *item, NSError *err)
{
if (err) {
NSLog(@"%@",[err localizedDescription]);
}
else {
imagesURLs = [[NSMutableArray alloc] initWithArray:item];
}
}];
NSLog(@"%@",imagesURLs); // (null) here :(
Ideas?
So, you have some confusion about how blocks work.
The
NSLogline is run long before the block is ever executed. YourimagesURLvariable is only filled out after your parse returns, which is being done asynchronously. Move thatNSLoginside the block and you should see what you’re expecting.