在数组中存储块 [英] Storing Blocks in an Array

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

问题描述

在 Objective-C 中,我知道块被认为是对象,所以我想知道是否可以将它们存储在数组中.这就引出了一个问题,块是第一类对象还是只是为了在对象之间传递它们而将它们视为对象?如果它们是一流的对象,那么它们不应该存储在数组中吗?

In Objective-C, I know that blocks are considered objects, so I was wondering if it was possible to store them in an array. This begs the question, are blocks first class objects or are they just treated like objects for the sake of passing them between objects? If they are first class objects, then shouldn't they be storable in arrays?

推荐答案

无需过多赘述,在 ARC 下,您现在可以像任何其他对象一样向集合添加块(参见讨论).

我在下面保留了原始答案,因为它包含一些有趣的技术细节.

I've left the original answer intact below, since it contains some interesting technical details.

这就引出了一个问题,块是第一类对象还是它们只是为了在它们之间传递它们而将它们视为对象对象?如果它们是一流的对象,那么它们不应该是可以存储在数组中吗?

This begs the question, are blocks first class objects or are they just treated like objects for the sake of passing them between objects? If they are first class objects, then shouldn't they be storable in arrays?

块是 Objective-C 对象,其行为与所有其他 NSObject 非常相似,但有几个关键区别:

Blocks are Objective-C objects that very much behave like every other NSObject, with a couple of key differences:

  • 块总是由编译器生成的.当执行通过块声明时,它们在运行时被有效地分配/初始化".

  • Blocks are always generated by the compiler. They are effectively "alloc/init"ed at runtime as execution passes over the blocks declaration.

块最初是在堆栈上创建的.如果 Block_copy() 或 copy 方法 将 Block 移动到堆中,如果 Block 的生命周期超过当前范围(参见下面的 ARC 点).

Blocks are initially created on the stack. Block_copy() or the copy method must be used to move the Block to the heap if the Block is to outlive the current scope (see ARC point below).

除了内存管理之外,块并没有真正的可调用 API.

Blocks don't really have a callable API beyond memory management.

要将块放入集合中,必须先复制它.总是.包括在 ARC 下. (见评论.)如果你不这样做,有风险,栈分配的 Block 将被autoreleased,你的应用程序稍后会崩溃.

To put a Block into a Collection, it must first be copied. Always. Including under ARC. (See comments.) If you don't, there is risk that the stack allocated Block will be autoreleased and your app will later crash.

复制基于堆栈的块也将复制所有捕获的状态.如果您要制作一个块的多个副本,复制一次,然后复制副本会更有效(因为复制副本只会增加保留计数,因为块是不可变的).

Copying a stack based block will copy all of the captured state, too. If you are making multiple copies of a block, it is more efficient to copy it once, then copy the copy (because copying the copy just bumps the retain count since Blocks are immutable).

在 ARC 下,从一个方法或函数返回一个 Block 正常工作";它会被自动复制到堆中,并且返回的实际上是一个自动释放的块(在某些情况下编译器可能会优化掉自动释放).即使使用 ARC,您仍然需要先复制块,然后再将其粘贴到集合中.

Under ARC, returning a Block from a method or function "just works"; it'll be automatically copied to the heap and the return will effectively be an autoreleased Block (the compiler may optimize away the autorelease in certain circumstances). Even with ARC, you still need to copy the block before sticking it into a collection.

我写了几篇博文,都提供了块介绍 和一些提示和技巧.你可能会觉得它们很有趣.

I've written a couple of blog posts both providing an introduction to blocks and some tips and tricks. You might find them interesting.

而且,是的,将它们添加到字典中非常有用.我写了几段代码,其中我将块放入字典中作为命令处理程序,其中键是命令名称.很方便.

And, yes, adding 'em to dictionaries is quite useful. I've written a couple of bits of code where I dropped blocks into dictionaries as command handlers where the key was the command name. Very handy.

这篇关于在数组中存储块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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