在不使用ARC时替换__weak [英] substituting for __weak when not using ARC

查看:106
本文介绍了在不使用ARC时替换__weak的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这行代码:

__weak NSBlockOperation *weakOperation = operation;

这会触发此编译器错误:

which is triggering this compiler error:

__weak attribute cannot be specified on automatic variable.

原因是我没有启用ARC(尚未准备好开关) 。所以从另一个StackOverFlow问题,我建议使用:

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; 

这使得错误消失,但是对于我使用它的上下文,如果感兴趣,请参阅此问题:如何取消NSOperationQueue )。

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).

所以我的问题是,在这个实例中,我可以用 __ weak 关键字替换掉这个警告?当我使用 __ weak 时,一切实际上都能正常工作,但我担心它不会阻挡未来的iOS版本。

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.

推荐答案

您不应担心iOS的未来版本,因为 __ weak

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

看看你的其他帖子告诉我,你的目标是避免 weakOperation

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.

请注意 __ block retain 行为在ARC和手动保留计数之间是不同的。

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

这篇关于在不使用ARC时替换__weak的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆