substituting for __weak when not using ARC

╄→гoц情女王★ 提交于 2019-12-21 05:22:14

问题


I have this line of code:

__weak NSBlockOperation *weakOperation = operation;

which is triggering this compiler error:

__weak attribute cannot be specified on automatic variable.

Reason for this is I don't have ARC enabled (not ready to make the switch just yet). So from another StackOverFlow question, I was recommended to use:

__unsafe_unretained NSBlockOperation *weakOperation = operation; 

Which makes the error go away, but for the context I'm using it, it's not working (see this question if interested: How to cancel NSOperationQueue).

So my question is, what I can substitute the __weak keyword with in this instance to get rid of this warning? Everything actually works correctly when I use __weak, but I'm afraid it won't hold up over future versions of iOS.


回答1:


You should not be worried about future versions of iOS because __weak is something interpreted by the compiler while producing code for you.

Looking at your other post suggests to me that your goal is to avoid weakOperation to be retained despite reference from within the block. In your specific case, where you don't use ARC, you can replace __weak by __block to indicate that your variable should not be retained during capture.

Note that the influence of__block on retain behavior is different between ARC and manual retain counting.



来源:https://stackoverflow.com/questions/14484964/substituting-for-weak-when-not-using-arc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!