Recursive Block Retain Cycles

前端 未结 6 994
心在旅途
心在旅途 2020-12-13 04:44

Will this lead to any sort of retain cycle? Is it safe to use?

__block void (^myBlock)(int) = [^void (int i)
{
    if (i == 0)
        return;

    NSLog(@\"         


        
6条回答
  •  南笙
    南笙 (楼主)
    2020-12-13 05:33

    I wanted a solution that gets no warnings, and in this thread https://stackoverflow.com/a/17235341/259521 Tammo Freese gives the best solution:

    __block void (__weak ^blockSelf)(void);
    void (^block)(void) = [^{
            // Use blockSelf here
    } copy];
    blockSelf = block;
        // Use block here
    

    His explanation makes perfect sense.

提交回复
热议问题