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[])(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 = ;
int (^getGlobalInt)(void) = ^{ return GlobalInt; };

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

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

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

    Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...

  2. (译文)IOS block编程指南 3 概念总览

    Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...

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

    Getting Started with Blocks(开始block) The following sections help you to get started with blocks usin ...

  4. iOS多线程编程指南

    iOS多线程编程指南(拓展篇)(1) 一.Cocoa 在Cocoa上面使用多线程的指南包括以下这些: (1)不可改变的对象一般是线程安全的.一旦你创建了它们,你可以把这些对象在线程间安全的传递.另一方 ...

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

    当应用程序生成一个新的线程的时候,该线程变成应用程序进程空间内的一个实体.每个线程都拥有它自己的执行堆栈,由内核调度独立的运行时间片.一个线程可以和其他线程或其他进程通信,执行I/O操作,甚至执行任何 ...

  6. iOS多线程编程指南(一)关于多线程编程(转)

    原文:http://www.dreamingwish.com/article/ios-multi-threaded-programming-a-multi-threaded-programming.h ...

  7. iOS ---Extension编程指南

    当iOS 8.0和OS X v10.10发布后,一个全新的概念出现在我们眼前,那就是应用扩展.顾名思义,应用扩展允许开发者扩展应用的自定义功能和内容,能够让用户在使用其他app时使用该项功能.你可以开 ...

  8. View Programming Guide for iOS ---- iOS 视图编程指南(五)---Animations

      Animations Animations provide fluid visual transitions between different states of your user inter ...

  9. View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views

    Views Because view objects are the main way your application interacts with the user, they have many ...

随机推荐

  1. AM335x Android eMMC mkmmc-android.sh hacking

    # AM335x Android eMMC mkmmc-android.sh hacking # # . 有空解读一下android的分区文件. # . 代码来源:https://github.com ...

  2. AtCoder Beginner Contest 057

    A题 分析:24进制转换 #include<iostream> using namespace std; int main() { int a,b; while(cin>>a& ...

  3. absolute 和float

    position:absolute与float:left是近亲 包裹性 包裹性换种说法就是让元素inline-block化,例如一个div标签默认宽度是100%显示的,但是一旦被absolute属性缠 ...

  4. 利用记事本和cmd进行java编程(从安装IDE--编译--运行)

    java 最大特点---跨平台 所谓的跨平台性,是指软件可以不受计算机硬件和操作系统的约束而在任意计算机环境下正常运行.这是软件发展的趋势和编程人员追求的目标.之所以这样说,是因为计算机硬件的种类繁多 ...

  5. HDU-5538 House Building

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  6. C++笔试题库之编程、问答题 150~200道

    151.写出判断ABCD四个表达式的是否正确, 若正确, 写出经过表达式中 a的值 int a = 4; (A)a += (a++); (B) a += (++a) ;(C) (a++) += a;( ...

  7. HDU6031:Innumerable Ancestors(二分+倍增数组)

    传送门 题意 n个点的图,有n-1条无向边,m个询问,每次询问 给出两个集合a和b,找到a的一个元素x,b的一个元素y,使得x和y的lca深度最大 分析 这道题如果直接暴力做,复杂度为O(mk1k2* ...

  8. POJ2369【循环节】

    题意: 给一个序列,他需要几步就能变成原来的序列. 思路: 那么就是找一下各个循环节(用dfs随便搞了-),求一下最小公倍数就好了. 贴一发挫code- //#include <bits/std ...

  9. poj2229【完全背包-规律Orz...】

    挑战DP 题意: 被组合数只能是2的整数幂,然后给出一个数问有多少种组合(mod1e10): 思路: 完全背包做啊-还是蛮简单的-(这里取膜要改成加法,省时间-) dp[i]代表对于j的方案数 贴一发 ...

  10. 关于 <meta name="viewport" content="width=device-width, initial-scale=1.0">的解释

    对于移动开发来讲,我还不太熟悉.今天在github上荡了一个系统源码,进行分析.发现其中有这样一段话: <meta name="viewport" content=" ...