Generic typeof for weak self references

前端 未结 10 899
误落风尘
误落风尘 2020-12-07 11:29

I am trying to figure out a way to use typeof to create a weak reference to self for use in blocks to avoid retain cycles.

When I first rea

10条回答
  •  没有蜡笔的小新
    2020-12-07 12:09

    The correct way to do this is

    __weak ActualClassName* weakSelf = self;
    

    Macros only make it unclear what the variable actually is, and what you're actually doing with it, in addition to adding non-portable meta-language to your code.

    If you need a more generic version of the class than ActualClassName provides, you aren't dealing with self anymore, since where self is defined, so is the class of self defined.

    In those cases, you should use the closest base class name in your inheritance tree, NSObject or better, never id, e.g.

    __weak MyBaseClassName* weakObject = object;
    

提交回复
热议问题