我正在搜索__weak
和__block
To ARC or not to ARC? What are the pros and cons?
并发现如果我使用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
的引用吗?我们通过避免*
来存储什么?
我不知道我是否遗失了某些东西,但我无法理解这一点。
答案 0 :(得分:2)
这与所有权说明符无关。只是typeof(self)
已经是一个指针,因为self
的类型是"指向GWTSDemandContactsController"的指针,即GWTSDemandContactsController *
。完全写出的类型包括*
。
指向的对象是GWTSDemandContactsController
,但变量self
是指向该对象的指针。