天天看點

對weak的了解

參考文章: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的位址相同