Explaining weak self-determination using __typeof

I found an odd line of code in a REActivityViewController project on GitHub and cannot figure it out. It seems like avoiding casting the value to the exact class that it is declared, but I don't know why this would be necessary. I wonder how important this is for inheritance.

Can someone explain why this will be done?

__typeof(&*self) __weak weakSelf = self; 

https://github.com/romaonthego/REActivityViewController/blob/master/REActivityViewController/REPocketActivity.m

I would make it clearer by declaring it that way ...

 id __weak weakSelf = self; 

And then in the block I can rewrite it as a strong link.

 REPocketActivity* __strong strongSelf = (REPocketActivity*)weakSelf; 

Then I would use strongSelf inside the block. When it goes out of scope, it safely removes a strong link.

+7
source share
2 answers

__typeof(self) is good for portability, since it is not tied to a particular class, however, the &* trick seems clearly redundant. As far as I know, in C and therefore Objective-C , &*ptr completely equivalent to ptr .

However, this may not be valid in other C-like languages, such as C++ , because statements can be overloaded and semantics may not be as simple as they seem. In fact, I saw that &* already in C++ applications, especially when working with iterators. I assume that the author has a C++ background and why he inserted this redundant construct.

In any case, I could be wrong, and I would like to hear a more sophisticated explanation, it is.

+4
source

Since the self type in the Objective-C method is always a pointer type, __typeof (& * self) is redundant even in Objective-C ++. __typeof (self) should always work.

0
source

All Articles