Objective-C中的__typeof(& * self)是什么意思?它与__typeof(self)有何不同?

时间:2018-01-31 02:27:51

标签: objective-c

我正在查看一些MVVM示例代码并遇到这一行:

__weak __typeof(&*self) weakSelf = self;
/*later used in some block*/

我了解__weaktypeof(self)的用途。为什么不使用__weak __typeof(self)__weak ViewController *weakSelf?用于什么参考和星号(&*self)?

2 个答案:

答案 0 :(得分:0)

__weak __typeof(&*self)weakSelf = self;

__weak HomeVC *weakSelf2 = self;

NSLog(@"%@", weakSelf);  // This gives you the reference of the View Controller Object
NSLog(@"%p", weakSelf);  // This gives you only the address of the View Controller Object
NSLog(@"%@", weakSelf2); // Same as 1
NSLog(@"%p", weakSelf2); // Same as 2
NSLog(@"%@", &*self);    // Same as 1
NSLog(@"%p", &*self);    // Same as 2

// In the above logs you can notice the difference just by changing the format specifier. Hope this helps.

答案 1 :(得分:0)

*&是一个C ++ - ism。它用于通过引用指针参数传递。

&*看起来有些人非常困惑,最终编写的代码实际上是无操作的。但是,如果那是Objective-C ++代码,那么&*可能会做一些事情(iirc,智能指针最终可能需要奇怪的间接使用来做某些事情)。

我工作的大部分项目都使用__weak typeof(self) weakSelf = self;