天天看點

OC—— 代碼塊

1

//代碼塊的定義

int (^max)(int , int );//向上的箭頭開頭 ,代碼塊的名字 max傳回值為int 類型;傳入參數 int,int 

//用函數閉包的方式指向一個閉包函數

max=^(int a,int b){

            return a>b?a:b;

        };

 printf("the max is %d \n",max(2,3));

程式運作結果:

the max is 3 

Program ended with exit code: 0

2 還可以給它指定一個類型(sayHello)的代碼塊

typedef void (^sayHello) ();   

 sayHello sh=^(){ //直接指派等于一個代碼塊

            printf("programing is fun \n");

        };

        sh();

 //sh 就可以直接執行這個方法

程式運作的結果:

programing is fun