iOS底层基础知识iOS开发攻城狮的集散地

为什么self.block里的typeof(self)不会造成循

2019-05-23  本文已影响113人  Lol刀妹

我们都写过weak strong dance:

__weak typeof(self) weakSelf = self;
self.block = ^{
    __strong typeof(self) strongSelf = weakSelf;
    strongSelf.view.backgroundColor = [UIColor greenColor];
};

有些人写的是__strong typeof(weakSelf) strongSelf = weakSelf;.

其实weakSelf和self都可以,都不会造成retain cycle。

那为什么self.block持有self却不会retain cycle呢?


思考分割线


下面是答案:

typeof is not a function, it's a keyword and isn't used at runtime at all. All __strong typeof(self) is doing here is telling the compiler how to evaluate the symbol strongSelf. It doesn't cause any runtime code to be generated, because it doesn't matter at runtime what that type actually is. All those decisions are made at compile-time.
This is the same as defining something as int x; The runtime does not in any way have a reference to "int". It's just a C type.

原问题在这:
https://stackoverflow.com/questions/55044299/block-didn-t-capture-self-in-typeof-why

上一篇下一篇

猜你喜欢

热点阅读