6

我有一个我使用的块,self所以我声明了一个对 self 的弱引用:

__weak MyClass *weakSelf = self;

现在我的问题:

  1. 我在定义的地方出现错误,我weakSelf不明白这应该是什么意思。:

    无法在自动变量上指定弱属性

  2. 在我的街区内,我传递weakSelf到另一个街区,我不确定我现在是否必须像这样再次做同样的事情:

    __weak MyClass *weakWeakSelf = weakSelf;
    

    然后传到weakWeakSelf那个街区?

4

3 回答 3

8

这很可能发生在您针对 iOS 4 时。您应该将其更改为

__unsafe_unretained MyClass *weakWeakSelf = weakSelf;
于 2012-05-03T12:28:13.470 回答
3

带弧

__weak __typeof__(self) wself = self;

没有ARC

__unsafe_unretained __typeof__(self) wself = self;
于 2013-01-31T22:29:13.053 回答
1

使用libextobjc,它将易于阅读且易于阅读:

- (void)doStuff
{
    @weakify(self); 
    // __weak __typeof__(self) self_weak_ = self;

    [self doSomeAsyncStuff:^{

        @strongify(self);
        // __strong __typeof__(self) self = self_weak_;

        // now you don't run the risk of self being deallocated
        // whilst doing stuff inside this block 
        // But there's a chance that self was already deallocated, so
        // you could want to check if self == nil

        [self doSomeAwesomeStuff];

        [self doSomeOtherAsyncStuff:^{

            @strongify(self);
            // __strong __typeof__(self) self = self_weak_;

            // now you don't run the risk of self being deallocated
            // whilst doing stuff inside this block 
            // Again, there's a chance that self was already deallocated, so
            // you could want to check if self == nil

            [self doSomeAwesomeStuff];

        }];
    }];
}
于 2016-05-21T08:57:16.397 回答