
[英]weak variable with a strong reference in a block: does not create a retain cycle?
[英]Why does using typeof() to create a weak reference does not require a pointer asterisk?
我在搜索__weak
和__block
之间的区别
并发现如果我使用的是ARC,则应在块中使用__weak
引用。
我的旧代码是这样的:
__block GWTSDemandContactsController *safeMe = self;
[GWTSService getSuggestedContactsForDemand:self.demand success:^(NSArray *contacts) {
safeMe.activityLoading.hidden = true;
[safeMe setContactsForView:contacts];
} failure:^(NSError *error) {
safeMe.activityLoading.hidden = true;
}];
然后,当我迁移到使用ARC时,我开始使用__weak
,还发现我可以使用typeof(self)
这非常简单,因此不必每次保存self
引用时都写类的名称。 所以现在我的代码看起来像这样:
__weak typeof(self) safeMe = self;
但是为什么我们在这里避免*
? 这不是对self
吗? 避免*
我们在这里存储什么?
我不知道我是否想念什么,但我听不懂。
这与所有权说明符无关。 只是typeof(self)
已经是一个指针,因为self
的类型是“指向GWTSDemandContactsController的指针”,即GWTSDemandContactsController *
。 完全写出的类型包括*
。
指向的对象是GWTSDemandContactsController
,但是变量self
是指向该对象的指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.