美文网首页iOS底层基础知识iOS开发攻城狮的集散地
为什么self.block里的typeof(self)不会造成循

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

作者: Lol刀妹 | 来源:发表于2019-05-23 15:53 被阅读113次

    我们都写过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

    相关文章

      网友评论

        本文标题:为什么self.block里的typeof(self)不会造成循

        本文链接:https://www.haomeiwen.com/subject/oqrtiqtx.html