(译文)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分享修改
  • 可以在词法作用域(栈)被销毁后继续分享和修改词法作用域中的状态。

你可以复制一个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

Previous

时间: 2024-10-06 03:29:08

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

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

Declaring and Creating Blocks (声明和创建blocks) Declaring a Block Reference (声明一个block引用) Block variables hold references to blocks. You declare them using syntax similar to that you use to declare a pointer to a function, except that you use ^ instead o

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

Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory. A block ca

IOS block编程指南 1 介绍

Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory. A block ca

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

Getting Started with Blocks(开始block) The following sections help you to get started with blocks using practical examples. 接下来这一节有实用的例子帮你开始blocks. Declaring and Using a Block (定义和使用block) You use the ^ operator to declare a block variable and to indic

IOS block编程指南 2 block开始

Getting Started with Blocks(开始block) The following sections help you to get started with blocks using practical examples. 接下来这一节有实用的例子帮你开始blocks. Declaring and Using a Block (定义和使用block) You use the ^ operator to declare a block variable and to indic

线程同步-iOS多线程编程指南(四)-08-多线程

首页 编程指南 Grand Central Dispatch 基本概念 多核心的性能 Dispatch Sources 完结 外传:dispatch_once(上) Block非官方编程指南 基础 内存管理 揭开神秘面纱(上) 揭开神秘面纱(下) iOS多线程编程指南 关于多线程编程 线程管理 Run Loop 线程同步 附录 Core Animation编程指南 Core Animation简介 基本概念 渲染架构 几何变换 查看目录 中文手册/API ASIHTTPRequest Openg

iOS ---Extension编程指南

当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他app时使用该项功能.你可以开发一个应用扩展来执行某些特定的任务,用户使用该扩展后就可以在多个上下文环境中执行该任务.比如说,你提供了一个能让用户把内容分享到社交网站的扩展,用户可以在阅读邮件信息或者上网时通过app扩展来发表评论.或者说,如果你提供了一个能展示当前比赛分数的扩展,那么用户可以把它放在通知中心,这样在他们打开To

iOS线程编程指南

原英文网址为: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html 同步 在应用程序中的多个线程的存在开辟了潜在的问题,关于安全访问到资源从多个执行线程.两个线程修改相同的资源可能会相互干扰,以意想不到的方式.例如,一个线程可能会覆盖其他人的更改或应用程序置于未知和潜在无效的状态.如果你很幸运,已损坏的资源可能会导致

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

当应用程序生成一个新的线程的时候,该线程变成应用程序进程空间内的一个实体.每个线程都拥有它自己的执行堆栈,由内核调度独立的运行时间片.一个线程可以和其他线程或其他进程通信,执行I/O操作,甚至执行任何你想要它完成的任务.因为它们处于相同的进程空间,所以一个独立应用程序里面的所有线程共享相同的虚拟内存空间,并且具有和进程相同的访问权限. 一.线程成本 多线程会占用你应用程序(和系统的)的内存使用和性能方面的资源.每个线程都需要分配一定的内核内存和应用程序内存空间的内存.管理你的线程和协调其调度所需