iOS block 內容詳解

2021-07-14 01:29:12 字數 4579 閱讀 3625

//

// viewcontroller.m

// test_addentrydictionary_01

//// created by jeffasd on 16/6/26.

//#import "viewcontroller.h"

typedef int(^myblock)(int, int);

typedef int(^tyblock)(int, int);

@inte***ce viewcontroller ()

@property (nonatomic, copy) tyblock tyblock;

@end

@implementation viewcontroller

- (void)viewdidload ];

//

// nslog(@"dfsfs");

// [self testasynctosync:^int(int a, int b) ];

//

// nslog(@"dfsfs");

dispatch_sync(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^];

nslog(@"result is %d", result);

});}- (int)testblocklocalvalue:(int(^)(int a, int b))myblock);

nslog(@"sencond");

nslog(@"a is %d", a);

return a;

}- (void)testasynctosync:(int(^)(int a, int b))myblock);

// //主線程中執行操作

// dispatch_async(dispatch_get_main_queue(), ^);

//在主線程中執行完操作後在子執行緒做更新ui後的操作

nslog(@"the therad is %@", [nsthread currentthread]);

nslog(@"the a is %d", a);

});}//不要這樣使用

- (int)testasynctosyncresult:(int(^)(int a, int b))myblock);

nslog(@"a1 is %d", a);

});nslog(@"a2 is %d", a);

return a;

}- (void)testfounction];

[self testsyncblock:^int(int a, int b) ];

nslog(@"test who is first");

nslog(@"--- thread is %@", [nsthread currentthread]);

nslog(@"sencond");

[self testsync_01];

}- (void)testsync_01);

[nsthread sleepfortimeinterval:2.0f];

nslog(@"模擬耗時完成 %@", [nsthread currentthread]);

dispatch_async(q, ^);

// [nsthread sleepfortimeinterval:2.0f];

//

// nslog(@"登入完成的處理 %@", [nsthread currentthread]);

}#pragma mark - 非同步block

- (void)testsyncblock:(int(^)(int a, int b))myblock);

dispatch_queue_t serialqueue = dispatch_queue_create("serial", null);

dispatch_queue_t concurrent = dispatch_queue_create("concurrent", dispatch_queue_concurrent);

// dispatch_sync(serialqueue, ^);

// dispatch_sync(concurrent, ^);

#if 1

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^);

dispatch_queue_t serialqueue = dispatch_queue_create("serial", null);

dispatch_queue_t concurrent = dispatch_queue_create("concurrent", dispatch_queue_concurrent);

dispatch_sync(serialqueue, ^);

// dispatch_sync(concurrent, ^);

});#endif

}- (void)testasyncblock1:(int(^)(int a, int b))myblock);

// dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^);

dispatch_queue_t serialqueue = dispatch_queue_create("serial", dispatch_queue_serial);

dispatch_queue_t concurrentqueue = dispatch_queue_create("concurrent", dispatch_queue_concurrent);

// dispatch_async(serialqueue, ^);

dispatch_async(concurrentqueue, ^);

}- (void)testasyncfounction);

dispatch_queue_t serialqueue = dispatch_queue_create("serial", dispatch_queue_serial);

dispatch_queue_t concurrentqueue = dispatch_queue_create("concurrent", dispatch_queue_concurrent);

dispatch_async(serialqueue, ^);

dispatch_async(concurrentqueue, ^);

dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^);

nslog(@"sencond");

}#pragma mark - 同步block

- (void)testsyncfounction);

dispatch_queue_t serialqueue = dispatch_queue_create("serial", null);

dispatch_queue_t concurrent = dispatch_queue_create("concurrent", dispatch_queue_concurrent);

dispatch_sync(serialqueue, ^);

dispatch_sync(concurrent, ^);

nslog(@"sencond");

}- (void)test mutablecopy];

nsdictionary *newdic = @;

[mutdic addentriesfromdictionary:newdic];

nslog(@"the dic is %@", mutdic);

int result = 0;

result = [self testblockresult:^int(int a, int b) ];

nslog(@"the result is %d", result);

// self testblocktypedef:^int(int, int)

self.tyblock = ^int(int a, int b);

}#pragma mark - 同步block 相當於函式呼叫

- (int)testblockresult:(int(^)(int a, int b))myblcok);

//

// return 3;

}- (int)testblock1:(void(^)(int, int))myblock

- (int)testblcokinfo:(void(^)(nsstring *, nsstring *))myblock

- (int)testblocktypedef:(myblock)myblock

- (void)touchesbegan:(nsset*)touches withevent:(uievent *)event

- (void)didreceivememorywarning

@end

IOS block學習筆記

天天看別人的block,各種羨慕嫉妒恨。狠下心來 自己也玩玩block,看看這水深不深。block 宣告格式 return type block name param type,param type,例 int personblock int,int block 定義格式 return type p...

IOS block簡單講解

block定義 int sum int a,int b int a,int b 返回型別 blockname 引數型別 引數1,引數型別 引數2 引數型別 引數1,引數型別 引數2 呼叫block int n n sum 10,15 nslog a b i n typedefint mysum in...

IOS block學習筆記

天天看別人的block,各種羨慕嫉妒恨。狠下心來 自己也玩玩block,看看這水深不深。block 宣告格式 return type block name param type,param type,例 int personblock int,int block 定義格式 return type p...