我有一个我使用的块,self所以我声明了一个对 self 的弱引用:
__weak MyClass *weakSelf = self;
现在我的问题:
我在定义的地方出现错误,我
weakSelf不明白这应该是什么意思。:无法在自动变量上指定弱属性
在我的街区内,我传递
weakSelf到另一个街区,我不确定我现在是否必须像这样再次做同样的事情:__weak MyClass *weakWeakSelf = weakSelf;然后传到
weakWeakSelf那个街区?
我有一个我使用的块,self所以我声明了一个对 self 的弱引用:
__weak MyClass *weakSelf = self;
现在我的问题:
我在定义的地方出现错误,我weakSelf不明白这应该是什么意思。:
无法在自动变量上指定弱属性
在我的街区内,我传递weakSelf到另一个街区,我不确定我现在是否必须像这样再次做同样的事情:
__weak MyClass *weakWeakSelf = weakSelf;
然后传到weakWeakSelf那个街区?
这很可能发生在您针对 iOS 4 时。您应该将其更改为
__unsafe_unretained MyClass *weakWeakSelf = weakSelf;
带弧
__weak __typeof__(self) wself = self;
没有ARC
__unsafe_unretained __typeof__(self) wself = self;
使用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];
}];
}];
}