Setting NSError within a block, using ARC

前端 未结 2 1717
无人共我
无人共我 2020-12-29 11:29

I wish to set an NSError pointer from within a block in a project using automatic reference counting. What follows is a simplified version of my code:

- (BOO         


        
2条回答
  •  长情又很酷
    2020-12-29 11:34

    Note: As of 2020, this workaround is no longer necessary.


    Try this:

    // ...
    __block NSError *blockError = nil;
    [items enumerateObjectsUsingBlock:^(id item, NSUInteger idx, BOOL *stop) {
        NSError *localError = nil;
        if (![blockSelf doSomethingWithItem:item error:&localError]) {
            blockError = localError;
        }
    }];
    // ...
    

    As for exactly why this is necessary, I'm still trying to get a grasp on that myself. I'll update this answer when I do. :)

提交回复
热议问题