__weak和__block引用之间有什么区别? [英] What is the difference between a __weak and a __block reference?

查看:167
本文介绍了__weak和__block引用之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Xcode的文档,这使我感到困惑:

I'm reading Xcode's documentation, and here is something that puzzles me:

__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
    [tmpSelf doSomething];
}];

以下内容是从文档中复制的:

The following is copied from the documentation:

块是对其捕获的变量的有力参考.如果您使用 self在一个块中,该块形成对self的强引用,因此如果 self对块也有很强的引用(通常是 的确如此),因此会产生较强的参考周期.为了避免周期,您需要 在块外创建对自身的弱引用(或__block),例如 在上面的示例中.

A block forms a strong reference to variables it captures. If you use self within a block, the block forms a strong reference to self, so if self also has a strong reference to the block (which it typically does), a strong reference cycle results. To avoid the cycle, you need to create a weak (or __block) reference to self outside the block, as in the example above.

我不明白弱者(或__block)"是什么意思?

I don't understand what does 'a weak (or __block)' mean?

__block typeof(self) tmpSelf = self;

__weak typeof(self) tmpSelf = self;

这里完全一样吗?

我在文档中发现了另外一块:

I found another piece in the document:

注意:在垃圾回收的环境中,如果同时应用__weak__block变量的修饰符,则该块将无法确保其保持活动状态.

Note: In a garbage-collected environment, if you apply both __weak and __block modifiers to a variable, then the block will not ensure that it is kept alive.

所以,我完全感到困惑.

So, I'm totally puzzled.

推荐答案

来自有关__block的文档

From the docs about __block

__ block变量存在于存储中,该存储在变量的词法范围与在该变量的词法范围内声明或创建的所有块和块副本之间共享.因此,如果在帧内声明的块的任何副本在帧的末尾都可以生存(例如,通过排队到某个地方以供以后执行),则该存储将在堆栈帧的破坏中幸免.给定词法范围内的多个块可以同时使用共享变量.

__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created within the variable’s lexical scope. Thus, the storage will survive the destruction of the stack frame if any copies of the blocks declared within the frame survive beyond the end of the frame (for example, by being enqueued somewhere for later execution). Multiple blocks in a given lexical scope can simultaneously use a shared variable.

来自有关__weak

From the docs about __weak

__ weak指定一个引用,该引用不能使引用的对象保持活动状态.如果没有对对象的强引用,则将弱引用设置为nil.

__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.

因此,它们在技术上是不同的. __block用于阻止将变量从外部作用域复制到块作用域. __weak是一个自定界的弱指针.

So they are technically different things. __block is to stop your variable being copied from your external scope into your block scope. __weak is a self delimiting weak pointer.

请注意,我从技术上说过,因为对于您的情况,他们将(几乎)做同样的事情.唯一的区别是您是否使用ARC.如果您的项目使用ARC,并且仅适用于iOS4.3及更高版本,请使用__weak.如果以某种方式释放了全局范围引用,它可以确保将引用设置为nil.如果您的项目不使用ARC或用于较早的OS版本,请使用__block.

Note I said technically, because for your case they will do (almost) the same thing. The only difference is if you are using ARC or not. If your project uses ARC and is only for iOS4.3 and above, use __weak. It ensures the reference is set to nil if the global scope reference is releases somehow. If your project doesn't use ARC or is for older OS versions, use __block.

这里有一个细微的差别,请确保您理解它.

There is a subtle difference here, make sure you understand it.

另一个难题是__unsafe_unretained.此修饰符与__weak几乎相同,但适用于4.3之前的运行时环境.但是,它没有设置为nil,可能会导致指针悬空.

Another piece to the puzzle is __unsafe_unretained. This modifier is almost the same as __weak but for pre 4.3 runtime environments. HOWEVER, it is not set to nil and can leave you with hanging pointers.

这篇关于__weak和__block引用之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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