https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Blocks/Articles/bxOverview.html#//apple_ref/doc/uid/TP40007502-CH3-SW1

Conceptual Overview

Block objects provide a way for you to create an ad hoc function body as an expression in C, and C-derived languages such as Objective-C and C++. In other languages and environments, a block object is sometimes also called a “closure”. Here, they are typically referred to colloquially as “blocks”, unless there is scope for confusion with the standard C term for a block of code.

Block Functionality

A block is an anonymous inline collection of code that:

  • Has a typed argument list just like a function

  • Has an inferred or declared return type

  • Can capture state from the lexical scope within which it is defined

  • Can optionally modify the state of the lexical scope

  • Can share the potential for modification with other blocks defined within the same lexical scope

  • Can continue to share and modify state defined within the lexical scope (the stack frame) after the lexical scope (the stack frame) has been destroyed

You can copy a block and even pass it to other threads for deferred execution (or, within its own thread, to a runloop). The compiler and runtime arrange that all variables referenced from the block are preserved for the life of all copies of the block. Although blocks are available to pure C and C++, a block is also always an Objective-C object.

Usage

Blocks represent typically small, self-contained pieces of code. As such, they’re particularly useful as a means of encapsulating units of work that may be executed concurrently, or over items in a collection, or as a callback when another operation has finished.

Blocks are a useful alternative to traditional callback functions for two main reasons:

  1. They allow you to write code at the point of invocation that is executed later in the context of the method implementation.

    Blocks are thus often parameters of framework methods.

  2. They allow access to local variables.

    Rather than using callbacks requiring a data structure that embodies all the contextual information you need to perform an operation, you simply access local variables directly.

最新文章

  1. iOS项目中安装和使用 Cocoapods
  2. C# 蓝牙编程
  3. css3 进度条
  4. asp.net中,<%#%>,<%=%>和<%%>分别是什么意思,有什么区别
  5. 我的android学习经历24
  6. 在mac上安装svn客户端
  7. 玩转Android---事件监听篇---第2篇
  8. linux下如何查询jdk安装路径
  9. jms的初步认识
  10. 201521123082《Java程序设计》第4周学习总结
  11. IEEE Trans 2009 Stagewise Weak Gradient Pursuits论文学习
  12. mysql大小写敏感(默认为1,不敏感)
  13. C#语法相比其它语言比较独特的地方
  14. PAT 甲级真题题解(1-62)
  15. angularjs的一些问题
  16. Android开发之自定义Toast(带详细注释)
  17. 对象的数据属性(Object)
  18. C# 实现快速闪电关机、快速重启
  19. FZU 2139——久违的月赛之二——————【贪心】
  20. 程序员学炒股(6) 让我们来看一下7月份A股的表现

热门文章

  1. Identity Server 4 原理和实战(完结)_Reference Token,撤销Tokens,Google和Github登录
  2. JAG Practice Contest for ACM-ICPC Asia Regional 2016B题【BFS】
  3. HDU 1556【线段树区间更新】
  4. laravel V层
  5. vue 脚手架 图片预加载
  6. 下载devc++和codeblocks记录
  7. java中数据的存放位置
  8. UILabel和UIbutton(富文本)封装方法
  9. JSON对象 JSON字符串 JSON数组
  10. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B