为什么使用typeof()创建弱引用不需要指针星号?

时间:2015-01-14 19:36:01

标签: objective-c pointers typeof variable-types

我正在搜索__weak__block

之间的区别

我的旧代码是这样的:

__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的引用吗?我们通过避免*来存储什么?

我不知道我是否遗失了某些东西,但我无法理解这一点。

1 个答案:

答案 0 :(得分:2)

这与所有权说明符无关。只是typeof(self)已经是一个指针,因为self的类型是"指向GWTSDemandContactsController"的指针,即GWTSDemandContactsController *。完全写出的类型包括*

指向的对象GWTSDemandContactsController,但变量self是指向该对象的指针。