为什么使用 typeof() 创建弱引用不需要指针星号?
Why does using typeof() to create a weak reference does not require a pointer asterisk?
我正在搜索 __weak
和 __block
之间的区别
- What is the difference between a __weak and a __block reference?
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)
- Generic typeof for weak self references
这个很简单,这样我就不用每次想保存self
引用的时候都写class的名字了。所以现在我的代码看起来像这样:
__weak typeof(self) safeMe = self;
但是这里为什么要避开*
呢?不应该是对 self
的引用吗?通过避免 *
?
我们在这里存储了什么
我不知道我是否遗漏了什么,但我无法理解这一点。
这与所有权说明符没有任何关系。只是typeof(self)
已经是指针了,因为self
的类型是"pointer to GWTSDemandContactsController",即GWTSDemandContactsController *
。完全写出的类型包括 *
.
对象 指向 是一个 GWTSDemandContactsController
,但变量 self
是指向该对象的指针。
我正在搜索 __weak
和 __block
- What is the difference between a __weak and a __block reference?
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)
- Generic typeof for weak self references
这个很简单,这样我就不用每次想保存self
引用的时候都写class的名字了。所以现在我的代码看起来像这样:
__weak typeof(self) safeMe = self;
但是这里为什么要避开*
呢?不应该是对 self
的引用吗?通过避免 *
?
我不知道我是否遗漏了什么,但我无法理解这一点。
这与所有权说明符没有任何关系。只是typeof(self)
已经是指针了,因为self
的类型是"pointer to GWTSDemandContactsController",即GWTSDemandContactsController *
。完全写出的类型包括 *
.
对象 指向 是一个 GWTSDemandContactsController
,但变量 self
是指向该对象的指针。