现在的位置: 首页 > 综合 > 正文

是否能将block储存在数组中?能

2018年08月28日 ⁄ 综合 ⁄ 共 2967字 ⁄ 字号 评论关闭

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

Block 是object-c中的对象,操作block很多情况下就像操作其他nsobject一样,但是有一些重要的不同:

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

    Block 通常由编译器生成,他们在程序运行时,执行到block的声明的时时候,被“alloc/init”

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

    Block 最初在栈里面被创建,当Block的生命周期比作用域要长的时候,需要将block挪到堆里面去,这时候就必须要使用Block_copy()这个copy方法

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

    Block在内存管理之外并没有可以调用的API  //这句话不知道神马意思

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

    要将Block放到一个收集容器里面,她首先必须要被copy。通常来说,这要在arc环境下面进行。如果你不这样搞,就会有一个风险:由stack allocate 的block将会被自动释放,然后你的app就妥妥的挂掉了

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

    copy一个在stack里面分配的block同时也将会copy她的所有状态(执行到哪一条语句,等等状态)。如果你想搞一推block的copy,更有效地方法是:先搞一个copy出来(copyBlock),然后再去copy搞出来的那个copyBlock(因为copy那个copyBlock仅仅是将retain count清零,因为block是不可变的)//不是很懂

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

    在arc环境下,从一个函数或者方法中返回一个block是“妥妥的行”的;她会被自动copy到堆里面,返回的block会被自动回收(编译器会使这个自动回收尽可能最优)。即使用上了arc,将block 放到容器之前仍然需要copy你的block,将copy放到容器里面

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

我搞了几篇屌炸天的blog,是关于 block的介绍 还有 一些技巧跟提示 。你可能会觉得它们很逗。

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.

http://stackoverflow.com/questions/7997666/storing-blocks-in-an-array

---------------------------------------------------------------分割线 2013.9.10-------------------------------------------------------------------------------------

Under ARC, a block does not have to be copied anymore when being added to a collection! Apple
said that in their "
ARC
Transition Guide
", but that guide is rather dated. Please check outblog.parse.com/2013/02/05/objective-c-blocks-quiz -
especially the answers to example B and example C, as well as the "
Conclusion"
at the end. The need for copying blocks even under ARC arose from a compiler bug in clang (which has been fixed long ago) and was just a work-around since Apple couldn't wait for this bug to be fixed first before releasing the next Xcode version.

在arc里面,将一个block放到容器之前已经不需要copy它鸟!!!Apple在”ARC Transition Guide“里面是这么扯的!但是那个Guide过期了?!你可以在这里搞到:blog.parse.com/2013/02/05/objective-c-blocks-quiz。特别是例子B跟例子C的答案,还有最后的结论。即使是在ARC里面,需要copy一下block才能放到容器里面这个鸡巴操作会引起编译器的错误。。。。。。后面省略

抱歉!评论已关闭.