ARC, self and blocks

后端 未结 3 1332
一个人的身影
一个人的身影 2021-02-06 06:34

I thought I understood the usage of self in a block that is copied is a no no.

But in an attempt to clean my code i enabled a bunch of warnings in Xcod

3条回答
  •  天命终不由人
    2021-02-06 07:00

    Remember that in ARC, an object will not be deallocated as long as there's a strong reference to it.

    When an object has a weak reference, the object might be deallocated (if there's no other strong reference to the same object), so a weak reference doesn't ensure the objects life.

    By doing this:

    typeof(self) selfref = weakself; 
    

    you're ensuring that you have a strong reference to that object before you use it (selfref is the strong reference pointing to the same object weakself is referring to). If you don't do this, the object could be deallocated while you use it.

提交回复
热议问题