block系列重新整理1

2021-06-26 13:50:45 字數 1660 閱讀 2697

main.m

#import #import "person.h"

int sum(int a, int b)

int main(int argc, const char * argv)

;

int (^myblock1)(int,int) = ^(int a, int b);

//呼叫block

result = myblock(10, 20);

printf("myblock:result:%d\n",result);

result = myblock1(6,3);

printf("myblock1:result:%d\n",result);

/*----------------演示void block-----------------*/

//1、宣告和實現

void (^voidblcok)() = ^;

//2、呼叫

voidblcok();

/*--------------person類中與main函式回掉------------*/

person *ps = [[person alloc] init];

[ps testmethod:^];

result = [ps testmethod1:^int(int a, int b) ];

printf("ps:testmethod1:result:%d\n",result);

result = [ps testmethod2:^int(int a, int b) ];

printf("ps:testmethod2:result:%d\n",result);

//實現block,最好直接按回車鍵,如果自己寫不太方便,容易出錯

personblock personblock = ^(int a, int b);

result = [ps testmethod2:personblock];

printf("ps:testmethod2:result:%d\n",result);

}return 0;

}

person.h

typedef int(^personblock)(int,int);

@inte***ce person : nsobject

//不帶引數也沒有返回值的block作為函式引數

- (void)testmethod:(void(^)())block;

//返回乙個整形,並且有兩個整形引數作為blok的引數

- (int)testmethod1:(int(^)(int,int))block;

//使用personblock型別做為引數

- (int)testmethod2:(personblock)block;

person.m

- (void)testmethod:(void(^)())block

- (int)testmethod1:(int(^)(int,int))block

- (int)testmethod2:(personblock)block

Block系列1 初識block

1 定義函式 1 函式 int sum int a,int b 2 宣告 2 宣告函式指標 將sum換成 p就能夠了 int p int a,int b 1 宣告block 將sum換成 myblock就能夠了 int myblock int a,int b 舉一反三 void myblock2 v...

重新整理按鈕 1

將它的值預設設定在介面上,重新整理時使用s 獲取查詢引數 string lineid request.getparameter productline string familyid request.getparameter productfamily string productid reques...

Block底層實現系列 一 基礎篇

通過這篇博文,你可以了解到不訪問 訪問外部變數,不修改外部變數的block被轉換到c 後的實現方法.void callblock void myblock void void testblock callblock testblock int main int argc,const char arg...