retainCount in blocks show extrange behavior

前端 未结 2 884
挽巷
挽巷 2021-01-26 22:04

I got the this code in a class:

- (void)cancel {
    if (_cancelBlock)
        _cancelBlock();
}
- (void)overrideCancelWithBlock:(void(^)(void))cancelBlock {
            


        
2条回答
  •  终归单人心
    2021-01-26 22:43

    _cancelBlock = [[cancelBlock copy] retain];
    

    That over-retains the block; just -copy it.


    Since Blocks can change form at runtime based upon actions taken and change type at compile time based on implementation details, the various Block classes generally treat retainCount as the nonsense generator that it is. In some cases, this will mean returning 1 always.

    Note that a retainCount of zero is a logical impossibility in all cases.

提交回复
热议问题