Manual retain with ARC

后端 未结 3 1375
孤独总比滥情好
孤独总比滥情好 2021-02-01 05:06

Before ARC I had the following code that retains the delegate while an async operation is in progress:

- (void)startAsyncWork
{
    [_delegate retain];
    // ca         


        
3条回答
  •  攒了一身酷
    2021-02-01 05:17

    I have occasionally needed to manually retain and release things (sometimes just for debugging) and came up with the following macros:

    #define AntiARCRetain(...) void *retainedThing = (__bridge_retained void *)__VA_ARGS__; retainedThing = retainedThing
    #define AntiARCRelease(...) void *retainedThing = (__bridge void *) __VA_ARGS__; id unretainedThing = (__bridge_transfer id)retainedThing; unretainedThing = nil
    

    This works by using the __bridge_retained and __bridge_transfer to cast things to and from (void *) which causes things to be retained, or to create a strong reference without calling retain.

    Have fun, but be careful!

提交回复
热议问题