Search code examples
objective-cpointerstypeofvariable-types

Why does using typeof() to create a weak reference does not require a pointer asterisk?


I was searching a bit about the difference between __weak and __block

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.


Solution

  • 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.