Objective-C:ARC中的块 [英] Objective-C: blocks in ARC

查看:105
本文介绍了Objective-C:ARC中的块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在过渡到ARC(自动引用计数),此文档非常有帮助:

I'm transitioning to ARC (Automatic Reference Counting) and this document was very helpful: Transitioning to ARC Release Notes. It says:

块在ARC中如何工作?

How do blocks work in ARC?

在ARC模式下(例如在返回中)向上传递堆栈时,阻止正常工作".您无需再致电阻止复制".将堆栈向下"传递到arrayWithObjects:以及执行保留的其他方法时,仍然需要使用[^ {} copy].

Blocks "just work" when you pass blocks up the stack in ARC mode, such as in a return. You don’t have to call Block Copy any more. You still need to use [^{} copy] when passing "down" the stack into arrayWithObjects: and other methods that do a retain.

有人可以详细说明一下还是举个例子?我知道这意味着通常即使在声明的范围之外使用该块也不再需要[块复制],因为ARC现在可以更聪明地处理它,但是当我想要进行复制时,我仍然需要复制它将块传递给保留它的函数.这是正确的吗?

Can somebody elaborate on this a little bit or give some example? I understood that to mean usually I don't need to do [block copy] anymore even if the block is used outside the declared scope as ARC now will take care of it more smartly, but I still need to copy it when I want to pass the block to functions that would retain it. Is this correct?

实际上,我已经对其进行了一些试验,到目前为止,即使我将块传递给数组而不进行复制,这似乎也没问题,所以这个问题就出现了.谢谢!

Actually I've experimented with it a little bit and so far it seems okay even if I pass blocks to arrays without copying, so the question. Thanks!

推荐答案

让我们从MRC(手动引用计数)的角度考虑这个问题.从函数返回堆栈块时,总是正确的做法返回它的副本(通常使用[[ copy] autorelease]).这是因为堆栈块的生存期就是该函数调用,该函数调用即将结束.因此,为了使返回的指针在返回后指向有效的块,您当然必须返回堆副本.

Let's consider the problem from an MRC (manual reference counting) perspective. When you are returning a stack block from a function, it is always the right thing to do to return a copy of it (usually with [[ copy] autorelease]). This is because the lifetime of the stack block is that function call, which is ending. So in order for the returned pointer to point to a valid block after returning, of course you have to return a heap copy.

但是,如何将块作为参数传递给函数或方法呢?您应该通过它的副本吗?有点复杂.我们知道,如果您需要将一个块存储起来以备以后使用,而该地方将使函数调用不再有效(例如在实例变量或全局变量等中),则需要存储该块的副本.但是我们怎么知道什么时候是真的(如果是真的,哪个函数(调用者/被调用者)负责复制它)?

However, what about for passing a block as an argument to a function or method. Should you pass a copy of it? It's a little more complicated. We know that, if you need to store a block for later use in a place that will outlive the function call (e.g. in an instance variable or global variable, etc.), you need to store a copy of the block. But how do we know when this is true (and if it is true, which function (caller/callee) is responsible for copying it)?

通常,当将其传递给参数为块类型的函数或方法时,则不需要复制它,因为作为带有块参数的函数,如果有块参数,则该函数负责复制它它需要将其存储起来以备后用.

Generally, when you pass it to a function or method whose parameter is of block type, then you don't need to copy it, because, as a function that takes a block parameter, that function is responsible for copying it if it needs to store it around for later.

但是如何将块传递给参数为非块类型(例如id)的函数或方法呢?然后,该函数不知道其参数是一个块,因此仅在需要将其存储以供以后使用时才保留该对象,而不复制它.在这种情况下,如果我们知道函数或方法将在以后存储对象(例如,使用-[NSMutableArray addObject:]),则应传递该块的副本.我们应该始终通过副本吗?不必要.如果我们知道函数或方法不会在以后存储该块(例如,仅打印对象),则复制该块的效率很低.

But what about passing a block to a function or method whose parameter is of non-block type (like id)? Then that function does not know that its argument is a block, and so will only retain the object if it needs to store it for later, not copy it. In this case, if we know that the function or method will store the object around for later (e.g. with -[NSMutableArray addObject:]), we should pass a copy of the block. Should we always pass a copy? Not necessarily. If we know the function or method will not store the block for later (e.g. to just print the object), it is inefficient to make a copy of the block.

在ARC中,编译器会尽力为您做更多的事情.因此,在返回堆栈块(传递")的情况下,编译器知道返回副本始终是正确的事情,因此它隐式地做到了.这就是为什么您不再需要自己做.

In ARC, the compiler tries to do as much as possible for you. So in the case of returning a stack block ("passing it up"), the compiler knows that returning a copy is always the right thing, so it implicitly does that. That's why you don't need to do it yourself anymore.

但是,为了将块传递给方法或函数(将其传递"),并非总是能够自动确定是否需要复制,并且额外的复制可能效率低下,因此编译器不需要自动执行任何操作;而且您程序员需要弄清楚.

However, for passing a block to a method or function ("passing it down"), it is not always possible to automatically determine whether copying is needed or not, and extra copying might be inefficient, so the compiler doesn't do anything automatically; and you the programmer needs to figure it out.

我已经注意到,在最新版本的编译器中,ARC在我认为更多的情况下复制了块,因此也许他们将ARC的实现更改为几乎总是复制块,以免出错.谨慎,他们认为性能成本并不重要.因此,现在可以将其直接传递给addObject:而无需显式复制.但是我相信ARC并非总是如此,并且对此语言也无法保证,否则这种行为将在将来持续.

I've noticed that in recent versions of the compiler, ARC copies blocks in a lot more situations that I thought, so perhaps they changed the implementation of ARC to pretty much always copy blocks, in order to err on the side of caution, and they believed that the performance cost is not as important. So it is possible that it will work now passing a block directly to addObject: without explicitly copying. But I believe that this was not always the case with ARC, and there is no guarantee in the language for this, or that this behavior will stay in the future.

这篇关于Objective-C:ARC中的块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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