国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

(譯文)IOSblock編程指南4聲明和創建blocks

2019-11-14 18:21:42
字體:
來源:轉載
供稿:網友

 

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

 

本文原創,轉載請注明出處:http://blog.csdn.net/zhenggaoxing/article/details/44308855


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥阳县| 陇南市| 平昌县| 格尔木市| 从化市| 玉屏| 尖扎县| 克山县| 师宗县| 蒲城县| 东乌珠穆沁旗| 琼结县| 大宁县| 乌鲁木齐县| 恩施市| 巴南区| 仁化县| 马鞍山市| 大余县| 慈溪市| 霍州市| 玉环县| 客服| 湖北省| 迁安市| 湛江市| 宜川县| 南安市| 泾阳县| 福州市| 南汇区| 那坡县| 皮山县| 高尔夫| 城固县| 寿宁县| 友谊县| 玉树县| 阳山县| 十堰市| 休宁县|