(译文)IOS block编程指南 3 概念总览
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分享修改
- 可以在词法作用域(栈)被销毁后继续分享和修改词法作用域中的状态。
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:
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.
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只需要直接访问本地变量就行了。
(译文)IOS block编程指南 3 概念总览的更多相关文章
- (译文)IOS block编程指南 4 声明和创建blocks
Declaring and Creating Blocks (声明和创建blocks) Declaring a Block Reference (声明一个block引用) Block variable ...
- (译)IOS block编程指南 1 介绍
Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...
- (译)IOS block编程指南 2 block开始
Getting Started with Blocks(开始block) The following sections help you to get started with blocks usin ...
- iOS多线程编程指南
iOS多线程编程指南(拓展篇)(1) 一.Cocoa 在Cocoa上面使用多线程的指南包括以下这些: (1)不可改变的对象一般是线程安全的.一旦你创建了它们,你可以把这些对象在线程间安全的传递.另一方 ...
- iOS多线程编程指南(一)关于多线程编程(转)
原文:http://www.dreamingwish.com/article/ios-multi-threaded-programming-a-multi-threaded-programming.h ...
- iOS ---Extension编程指南
当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他app时使用该项功能.你可以开 ...
- iOS多线程编程指南(二)线程管理
当应用程序生成一个新的线程的时候,该线程变成应用程序进程空间内的一个实体.每个线程都拥有它自己的执行堆栈,由内核调度独立的运行时间片.一个线程可以和其他线程或其他进程通信,执行I/O操作,甚至执行任何 ...
- iOS 并发编程指南
iOS Concurrency Programming Guide iOS 和 Mac OS 传统的并发编程模型是线程,不过线程模型伸缩性不强,而且编写正确的线程代码也不容易.Mac OS 和 iOS ...
- iOS Threading编程指南 官方文档翻译第一篇(序言)
序言 Thread是能够使多个code paths 在同一个APP内并发运行的几种技术之一.虽然新的技术为并发运行提供了先进.高效的工具(例如operation 对象和GCD),但是OS X和iO ...
随机推荐
- JS 之正则表达式
字符 含意 \ 做为转意,即通常在"\"后面的字符不按原来意义解释,如/b/匹配字符"b",当b前面加了反斜杆后/\b/,转意为匹配一个单词的边界. -或- 对 ...
- 杂项-公司:Aspose
ylbtech-杂项-公司:Aspose Aspose 于2002年3月在澳大利亚悉尼创建,旗下产品覆盖文档.图表.PDF.条码.OCR.CAD.HTML.电子邮件等各个文档管理领域,为全球.NET ...
- gulp 实现 js、css,img 合并和压缩(转)
前提条件,知道如何安装nodejs.gulp,这里不做介绍,可以自行google 实现此功能需要安装的gulp工具有如下 npm install gulp-htmlmin gulp-imagemin ...
- PhpStorm之设置字体大小
1.点击左上角的File,再点击setting:(Ctrl+Alt+S) 2.进入 Editor / General,选择 Change font size (Zoom) with Ctrl+Mo ...
- poj1651【区间DP·基础】
题意: 给你一串数字,头尾不能动,每次取出一个数字,这个数字贡献=该数字与左右相邻数字的乘积,求一个最小值. 思路: 用dp[s][t]去代表s到t的最小值,包括a[s]和a[t],然后从区间为3开始 ...
- bzoj 1874: [BeiJing2009 WinterCamp]取石子游戏【博弈论】
先预处理出来sg值,然后先手必败状态就是sg[a[i]]的xor和为0(nim) 如果xor和不为0,那么一定有办法通过一步让xor和为0,具体就是选一个最大的sg[a[i]],把它去成其他sg值的x ...
- IDEA thymeleaf ${xxx.xxx}表达式报错,红色波浪线
解决方法: 在<!DOCTYPE html>标签下面加上 <!--suppress ALL--> 代码如图: 不再报错,效果如下图:
- pip 国内安装镜像源
转自: https://blog.csdn.net/xuezhangjun0121/article/details/81664260 pip国内的一些镜像 阿里云 http://mirrors.a ...
- 揭开Python科学计算的面纱
春牛春杖.无限春风来海上.便与春工.染得桃红似肉红. 春幡春胜.一阵春风吹酒醒.不似天涯.卷起杨花似雪花. 标准的Python中用列表保存一组值,可以当做数组使用,但是由于其值类型任意,所以列表中保存 ...
- spring cloud feign 调用接口报错"No message available
There was an unexpected error (type=Internal Server Error, status=500). status 404 reading HelloServ ...