(译文)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 of *. The block type fully interoperates with the rest of the C type system. The following are all valid block variable declarations:

block变量维持了一个对block的引用。你声明block使用了和声明函数指针相同的语法,除了你使用“^”代替了“*”之外。block类型可以和全部C类型系统相互操作。下列都是block变量的声明:

void (^blockReturningVoidWithVoidArgument)(void);
int (^blockReturningIntWithIntAndCharArguments)(int, char);
void (^arrayOfTenBlocksReturningVoidWithIntArgument[10])(int);

Blocks also support variadic (...) arguments. A block that takes no arguments must specify void in the argument list.

Blocks are designed to be fully type safe by giving the compiler a full set of metadata to use to validate use of blocks, parameters passed to blocks, and assignment of the return value. You can cast a block reference to a pointer of arbitrary type and vice versa. You cannot, however, dereference a block reference via the pointer dereference operator (*)—thus a block‘s size cannot be computed at compile time.

You can also create types for blocks—doing so is generally considered to be best practice when you use a block with a given signature in multiple places:

blocks  也支持可变参数。一个没有参数的block必须在参数列表中指定void。

blocks 被设计成对编译器的完全安全类型,它有一套完整的数据源设置来检测block的合法性,通过传给blocks参数,来分配返回值。你可以给block创建任意的指针类型,反之亦然(PS:这句话,翻译有疑问)。尽管如此,你不能通过解引用操作符(*)来解引用一个block——因为这样在编译的时候无法计算block的大小。

你也可以创建block作为类型,当你要在多个地方使用同一block签名的block的时候,这是通常情况下最好的方法。

typedef float (^MyBlockType)(float, float);

MyBlockType myFirstBlock = // ... ;
MyBlockType mySecondBlock = // ... ;

Creating a Block(创建一个block)

You use the ^ operator to indicate the beginning of a block literal expression. It may be followed by an argument list contained within (). The body of the block is contained within {}. The following example defines a simple block and assigns it to a previously declared variable (oneFrom)—here the block is followed by the normal ; that ends a C statement.

你使用^操作指示一个block表达的开始。也许还会有一个()包裹的参数列表。block的主体包含在{}中。下面的例子定义了一个简单的block分配给一个已经存在的变量(oneFrom)——这里的block是正常的,以C语言做结。

float (^oneFrom)(float);

oneFrom = ^(float aFloat) {
    float result = aFloat - 1.0;
    return result;
};

If you don’t explicitly declare the return value of a block expression, it can be automatically inferred from the contents of the block. If the return type is inferred and the parameter list is void, then you can omit the (void) parameter list as well. If or when multiple return statements are present, they must exactly match (using casting if necessary).

如果你没有明确的声明block表达式的返回值,系统可以根据block的内容推断。如果返回类型推断好,且参数列表为空,然后你也可以忽略参数列表。如果需要很多返回值,他们需要寄去匹配(如果必要可以进行类型转换)。

Global Blocks(全局block)

At a file level, you can use a block as a global literal:

在文件层面,你可以把block作为全局变量。

#import <stdio.h>

int GlobalInt = 0;
int (^getGlobalInt)(void) = ^{ return GlobalInt; };

NextPrevious

本文原创,转载请注明出处:http://blog.csdn.net/zhenggaoxing/article/details/44308855

时间: 2024-07-30 18:36:28

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

(译)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编程指南 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 “

(译)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编程指南 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多线程编程指南(四)-08-多线程

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

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

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

iOS线程编程指南

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

iOS ---Extension编程指南

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