Block

1、Block 語法

一、^ 返回值類型 參數列表 表達式

例如:函數

^int (int count) {return count + 1;}spa

 

二、^  參數列表 表達式

省略返回值時,若是表達式中有return語句就使用該返回值的類型,若是表達式中沒有return語句就使用void類型,表達式中含有多個return語句時,全部的返回值類型必須相同指針

^int (int count) {return count + 1;}變量

該源代碼可省略爲以下形式:語法

^ (int count) {return count + 1;}語言

該 Block 語法,返回 int 型返回值。co

 

三、^  表達式

若是不使用參數,參數列表也能夠省略源代碼

^void (void){printf("Block\n");}block

該源代碼可省略爲以下形式:background

^{printff("Block\n");}

 

2、 block類型變量

函數指針:int (*funcprt)(int) ;

block類型變量:int (^blk)(int);

聲明block類型變量僅僅是將聲明函數指針類型變量的 "*" 改成 "^",與通常 C 語言變量完成相同。

一、使用 Block 語法將 Block 賦值爲 Block 類型變量。

int (^blk)(int) = ^(int count){return count + 1;};

二、做爲函數的參數

void func (int (^blk)(int)){.... }

三、做爲函數的返回值

int (^func())(){

    return ^(int count){return count +1;};

}

使用 typedef

typedef int(^blk_t)(int);

一、做爲函數的參數

void func (blk_t){...}

二、做爲函數的返回值

blk_t func1(){

    return ^(int count){return count +1;};

}

相關文章
相關標籤/搜索