(譯文)IOS block編程指南 4 聲明和建立blocks

來源:互聯網
上載者:User

標籤:

 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

(譯文)IOS block編程指南 4 聲明和建立blocks

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.