参考文章:http://blog.cocoachina.com/article/61909
__weak typeof(self) weakSelf = self;
weak不会使strongSelf的引用计数增加,而是把strongSelf对象指针(weakSelf和strongSelf指针相同)加到了weak的hash表中,以便后续使用weakSelf时能找到
测试代码如下:
- (void)test{
TPLog(@"self === %@", self);
typeof(self) strongSelf = self;
TPLog(@"strong self === %@", strongSelf);
__weak typeof(self) weakSelf = self;
TPLog(@"weak self === %@", weakSelf);
}
打印结果为:
2019-02-22 23:41:19.771670+0800 TPWeiShangTool[17537:832581] self === <AppDelegate: 0x60000141c8c0>
2019-02-22 23:41:19.771817+0800 TPWeiShangTool[17537:832581] strong self === <AppDelegate: 0x60000141c8c0>
2019-02-22 23:41:39.853423+0800 TPWeiShangTool[17537:832581] weak self === <AppDelegate: 0x60000141c8c0>
可见strongSelf和weakSelf的地址相同