嵌套块和引用自我
我有一个块,我使用self
所以我宣布对自我的一个弱引用:
__weak MyClass *weakSelf = self;
现在我的问题:
-
我在定义
weakSelf
遇到一个错误,我不明白这应该是什么意思。无法在自动variables上指定弱属性
-
在我的街区里面,我将
weakSelf
传递给另一个街区,我不知道现在是否必须再次做同样的事情:__weak MyClass *weakWeakSelf = weakSelf;
然后把
weakWeakSelf
传给那个块?
这是最有可能发生,因为你是针对iOS 4。你应该改变它
__unsafe_unretained MyClass *weakWeakSelf = weakSelf;
用ARC
__weak __typeof__(self) wself = self;
没有弧
__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]; }]; }]; }