(译文)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:html

block变量维持了一个对block的引用。你声明block使用了和声明函数指针相同的语法,除了你使用“^”代替了“*”以外。block类型能够和所有C类型系统相互操做。下列都是block变量的声明ios

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.express

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:app

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

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

你也能够建立block做为类型,当你要在多个地方使用同一block签名的block的时候,这是一般状况下最好的方法。 ui

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.spa

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

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; };

 

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

相关文章
相关标签/搜索