本帖最后由 wslinke 于 2015-4-18 17:59 编辑
今天在学习block的时候有诸多疑惑,就硬着头皮翻看了苹果官方文档中block的有关部分
我翻译了一部分出来和大家分享一下
鉴于英语水平和专业水准有限,可能存在一些错误,欢迎大家指正
Working with Blocks
An Objective-C class defines an object that combines data with related behavior.
(Block是)一个OC类定义的,结合了数据与相关行为的对象。
Sometimes, it makes sense just to represent a single task or unit of behavior, rather than a collection of methods.
有时,在表示单个任务或单元的行为时,比起方法的集合,Block会更加适合.
Blocks are a language-level feature added to C, Objective-C and C++, which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values.
Blocks作为语言级别的特征被添加到C,OC和C++中,它允许你创建不同的,可以被传递到方法和函数中的的代码片段,就像是数值一样.
Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary. They also have the ability to capture values from the enclosing scope, making them similar to closures or lambdas in other programming languages.
Blocks是OC对象,这意味着它可以被加入OC集合中,比如NSArray、NSDictionary,它还有从封闭范围中获取值的能力,这类似于其他程序设计语言中的closures或lambdas.
This chapter explains the syntax to declare and refer to blocks, and shows how to use blocks to simplify common tasks such as collection enumeration. For further information, see Blocks Programming Topics .
这章解释了blocks的声明和引用,还展示了怎样使用blocks去简化一些常见的任务,比如列举集合中的元素.
Block Syntax
The syntax to define a block literal uses the caret symbol (^), like this:
定义一个Block的语法是使用插入符号^,就像这样
- ^{
- NSLog(@”This is a block”);
- }
复制代码
As with function and method definitions, the braces indicate the start and end of the block. In this example, the block doesn’t return any value, and doesn’t take any arguments.
正如函数和方法的定义一样,花括号指示了block的开始和结束.在这个例子中,block没有返回任何值,也没有接受任何参数.
In the same way that you can use a function pointer to refer to a C function, you can declare a variable to keep track of a block, like this:
就像可以使用一个函数指针去引用C函数一样,你也可以声明一个变量去保存block的踪迹,就如下面一样:
- void (^simpleBlock)(void);
复制代码
If you’re not used to dealing with C function pointers, the syntax may seem a little unusual.
如果你不习惯于使用C函数指针,这种语法可能会让你感觉不太寻常.
This example declares a variable called simpleBlock to refer to a block that takes no arguments and doesn’t return a value, which means the variable can be assigned the block literal shown above, like this:
这个例子声明了一个名为变量simpleBlock的变量,来引用一个不接受任何参数也不返回任何值的block,这意味着可以将上面的那个block赋给这个变量,就像这样:
- simpleBlock = ^{
- NSLog(@”This is a block”);
- };
复制代码
This is just like any other variable assignment, so the statement must be terminated by a semi-colon after the closing brace.
就像是其他变量赋值一样,这个声明必须在括号后面加上分号来结束;
You can also combine the variable declaration and assignment:
你也可以同时对在声明这个变量的同时对它赋值,就像这样:
- void (^simpleBlock)(void) = ^{
- NSLog(@"This is a block");
- };
复制代码
Once you’ve declared and assigned a block variable, you can use it to invoke the block:
当你已经对一个block变量进行声明和赋值,你就可以使用下面这种方式来调用这个block
simpleBlock();
Note: If you attempt to invoke a block using an unassigned variable (a nil block variable ),your app will crash.
注意,如果你试图调用一个没有赋值的变量(一个空的block变量),你的程序会奔溃.
Blocks Take Arguments and Return Values
Blocks can also take arguments and return values just like methods and functions.
就像方法和函数一样,Blocks同样可以接受参数和返回值.
As an example, consider a variable to refer to a block that returns the result of multiplying two values:
下面是一个例子,声明了一个变量来引用返回2个数乘积的block
- double (^multiplyTwoValues)(double, double);
复制代码
The corresponding block literal might look like this:
与之匹配的block可能是这样的:
- ^ (double firstValue, double secondValue) {
- return firstValue * secondValue;
- }
复制代码
The firstValue and secondValue are used to refer to the values supplied when the block is invoked, just like any function definition. In this example, the return type is inferred from the return statement inside the block.
firstValue 和 secondValue是用来引用这个block被调用时传入的参数,就像函数定义那样.在这个例子中,返回值的类型是根据block中的返回语句推断的.
If you prefer, you can make the return type explicit by specifying it between the caret and the argument list:
如果你愿意,你可以在插入符号^和参数列表之间指定返回值的类型
- ^ double (double firstValue, double secondValue) {
- return firstValue * secondValue;
- }
复制代码
Once you’ve declared and defined the block, you can invoke it just like you would a function:
一旦你已经声明和定义了block,你就可以想调用函数一样来调用它.
- double (^multiplyTwoValues)(double, double) =
- ^(double firstValue, double secondValue) {
- return firstValue * secondValue;
- };
- double result = multiplyTwoValues(2,4);
- NSLog(@"The result is %f", result);
复制代码
|
|