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对象给你提供了创建C语言和C语言拓展语言,比如objective-C和C++中的 ad hoc函数的可能。在其他语言环境中,block对象有时也被称作“closure”,这里呢,通常用大白话称为:“block”,除非作用域中有复杂的标准C block代码。

Block Functionality(block 功能)

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.

一个block是一个匿名内联代码集:

  • 像函数一样有一个类型参数列表。
  • 有一种推断或声明的返回值类型
  • 可以在它定义的词法范围内捕获状态
  • 可以随意的在词法作用域里面修改状态
  • 可以和相同作用域的block分享修改
  • 可以在词法作用域(栈)被销毁后继续分享和修改词法作用域中的状态。
你可以复制一个block甚至将它延期到其他线程执行(或者在自己的进程中加入一个循环队列)。编译器和运行时安排所有block的相关变量保存在所有block的拷贝的生命周期中。尽管block可以在纯C和C++中使用,在objective-C中也经常使用block对象。

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.

block通常表现为小的独立的代码块。因此呢,block经常被用于作为一个可能同时执行的封装单元,或者集合中的项,或者当操作结束时候的回调。
blocks 因为如下两个原因成为回调函数的一个有效替代者:
  1. 他们允许你在调用点(函数内容已经结束啦)之后写代码。因此呢block常常作为框架函数的参数出现。
  2. 他们允许你访问本地变量。相比于回调函数要求操作的所有上下文信息,block只需要直接访问本地变量就行了。
本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44305689

(译文)IOS block编程指南 3 概念总览的更多相关文章

  1. (译文)IOS block编程指南 4 声明和创建blocks

    Declaring and Creating Blocks (声明和创建blocks) Declaring a Block Reference (声明一个block引用) Block variable ...

  2. (译)IOS block编程指南 1 介绍

    Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...

  3. (译)IOS block编程指南 2 block开始

    Getting Started with Blocks(开始block) The following sections help you to get started with blocks usin ...

  4. iOS多线程编程指南

    iOS多线程编程指南(拓展篇)(1) 一.Cocoa 在Cocoa上面使用多线程的指南包括以下这些: (1)不可改变的对象一般是线程安全的.一旦你创建了它们,你可以把这些对象在线程间安全的传递.另一方 ...

  5. iOS多线程编程指南(一)关于多线程编程(转)

    原文:http://www.dreamingwish.com/article/ios-multi-threaded-programming-a-multi-threaded-programming.h ...

  6. iOS ---Extension编程指南

    当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他app时使用该项功能.你可以开 ...

  7. iOS多线程编程指南(二)线程管理

    当应用程序生成一个新的线程的时候,该线程变成应用程序进程空间内的一个实体.每个线程都拥有它自己的执行堆栈,由内核调度独立的运行时间片.一个线程可以和其他线程或其他进程通信,执行I/O操作,甚至执行任何 ...

  8. iOS 并发编程指南

    iOS Concurrency Programming Guide iOS 和 Mac OS 传统的并发编程模型是线程,不过线程模型伸缩性不强,而且编写正确的线程代码也不容易.Mac OS 和 iOS ...

  9. iOS Threading编程指南 官方文档翻译第一篇(序言)

    序言   Thread是能够使多个code paths 在同一个APP内并发运行的几种技术之一.虽然新的技术为并发运行提供了先进.高效的工具(例如operation 对象和GCD),但是OS X和iO ...

随机推荐

  1. 使用cocoaPods加载框架的具体步骤:

    注意事项: 1.使用之前备份一下代码.因为pod更新很快,如果某个文件名有中文,pod install 一下.整个项目可能就要废掉了. 2.如果不把pod文件推动到远程服务器. 每一次用的时候在本地p ...

  2. [SoapUI] Jenkins 配置

    cd %WORKSPACE% cmd /c call "%SOAPUI_PRO_HOME%\bin\testrunner.bat" -a -j -PprojectPath=&quo ...

  3. 【Codeforces 632D】 Longest Subsequence

    [题目链接] 点击打开链接 [算法] 设取的所有数都是k的约数,则这些数的lcm必然不大于k. 对于[1, m]中的每个数,统计a中有多少个数是它的约数即可. [代码] #include<bit ...

  4. 【BZOJ 3223】 文艺平衡树

    [题目链接] 点击打开链接 [算法] 本题是splay区间操作的模板题 我们每个点的权值设为”当前在序列中的排名“,根据二叉排序树的性质,这棵树的中序遍历就是当前序列 如果我们要获得一段区间[l,r] ...

  5. react之入门

    react出于FackBook,是一个将js与css共写的里程碑,主要用于构建UI,很多人认为 React 是 MVC 中的 V(视图),后来出现redux更多的是处理数据,所以也适合做逻辑复杂的管理 ...

  6. servlet中的servletURL,servletURI和servletPath

    String    servletURL=request.getservletURL(); url:站点名+当前web应用名+(目录名)+页面名 String    servletURI=reques ...

  7. Android Studio 1.5.1

    Android Studio 1.5.1 December 3rd, 2015: For information on what's new in 1.5.1, see the release ann ...

  8. 【旧文章搬运】炉子给的SYSTEM_HANDLE_TYPE有点错误

    原文发表于百度空间,2008-12-03========================================================================== 今天写程序 ...

  9. 2、HTML的head内标签

    一.Meta(metadata information) 提供有关页面的元信息,例:页面编码.刷新.跳转.针对搜索引擎和更新频度的描述和关键词 1.页面编码(告诉浏览器是什么编码) <meta ...

  10. hibernate的基础学习--多对多关联

    多对多采用学生老师模型进行测试 学生配置文件: <?xml version="1.0" encoding="utf-8" ?> <!DOCTY ...