I was searching a bit about the difference between __weak
and __block
To ARC or not to ARC? What are the pros and cons?
and found that if I am using ARC, I should use __weak
references in blocks.
My old code was something like this:
__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;
}];
Then when I migrated to use ARC, I started using __weak
and also found out that I could use typeof(self)
This is very simple, so that I don't have to write the name of the class every time I want to save the self
reference. So now my code looks like this:
__weak typeof(self) safeMe = self;
But why do we avoid the *
here? Shouldn't it be a reference to self
? What are we storing here by avoiding the *
?
I don't know if I am missing something or not, but I could not understand this.
This doesn't have anything to do with the ownership specifiers. It's just that typeof(self)
is already a pointer, because self
's type is "pointer to GWTSDemandContactsController", i.e., GWTSDemandContactsController *
. The fully-written-out type includes the *
.
The object pointed to is a GWTSDemandContactsController
, but the variable self
is a pointer to that object.