1.理論
Block (閉包): A Block is an anonymous inline collection of code . and sometimes also called a "closure".
閉包:能夠讀取其它函數內部變數的函數。
2.example
1)TestBlock.h
#import <Foundation/Foundation.h>
typedef void(^Block)(void);
typedef void(^Block_3)(void);
int par;
@interface TestBlock : NSObject
{
Block_3 block_3;
}
+ (void)testBlock_1:(Block)_block;
+ (void)testBlock_2:(int(^)(int))_block;
- (void)setBlock_3:(Block_3)newBlock;
- (void)callBlock_3;
@end
2) TestBlock.m
#import "TestBlock.h"
@implementation TestBlock
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
+ (void)testBlock_1:(Block)_block;
{
//回呼函數嘛,當然是要在函數內讓Block實現一次
_block();
NSLog(@"注意調用的順序");
}
+ (void)testBlock_2:(int(^)(int))_block
{
int result = _block(par);
NSLog(@"_______result:%d", result);
}
- (void)setBlock_3:(Block_3)newBlock
{
[block_3 release];
block_3 = [newBlock copy];
}
- (void)callBlock_3
{
block_3();
}
- (void)dealloc
{
[block_3 release], block_3 = nil;
[super dealloc];
}
@end
3)調用
TestBlock *testBlock;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_testBlock = [[TestBlock alloc] init];
/*==============================parting line==============================*/
int (^myblock_1)(int a, int b) = ^(int a, int b){
return a + b;
};
NSLog(@"------%d", myblock_1(1, 2));
/*==============================parting line==============================*/
//__block 變數
__block int num;
int (^myblock_2)(int a, int b) = ^(int a, int b){
num = a + b;
return num;
};
NSLog(@"++++++%d", myblock_2(2, 3));
/*==============================parting line==============================*/
//Block實現回調
self.button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame = CGRectMake(100, 100, 100, 50);
[self.button setTitle:@"TEST" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
/*==============================parting line==============================*/
//Block傳遞函數指標
int (^test_1)()= ^{
NSLog(@"I");
return 1;
};
int (^test_2)()= ^{
NSLog(@"MANNA");
return 2;
};
int (^test_3)()= ^{
NSLog(@"PLAY");
return 3;
};
[self testBlock:test_1];
[self testBlock:test_2];
[self testBlock:test_3];
/*==============================parting line==============================*/
//多Block參數
[self testBlock_Concurrency:^{
NSLog(@"這是第一個Block.");
return 100;
}block_2:^(int p){
NSLog(@"這是第二個Block.");
}block_3:^{
NSLog(@"這是第三個Block.");
}];
}
- (void)test:(id)sender
{
[TestBlock testBlock_1:^{
NSLog(@"FUCK U MAN.");
}];
//使用類方法實現調用
par = 100;
[TestBlock testBlock_2:^(int par){
NSLog(@"++++++++++");
return par - 1;
}];
//使用執行個體方法實現調用
[self.testBlock setBlock_3:^{
NSLog(@"this is block_3.");
}];
[self.testBlock callBlock_3];
}
- (void)testBlock:(int (^)())test_block
{
int result = test_block();
NSLog(@"注意先後順序,結果是:%d", result);
}
- (void)testBlock_Concurrency:(int (^)())block_1 block_2:(void(^)(int))block_2 block_3:(void(^)())block_3
{
int result = block_1();
block_2(100);
block_3();
NSLog(@"多麼神奇的Block.result=%d.", result);
}
4)測試結果
2013-05-08 10:26:30.309 TEST_test_01[571:11303] ------3
2013-05-08 10:26:30.310 TEST_test_01[571:11303] ++++++5
2013-05-08 10:26:30.312 TEST_test_01[571:11303] I
2013-05-08 10:26:30.312 TEST_test_01[571:11303] 注意先後順序,結果是:1
2013-05-08 10:26:30.313 TEST_test_01[571:11303] MANNA
2013-05-08 10:26:30.313 TEST_test_01[571:11303] 注意先後順序,結果是:2
2013-05-08 10:26:30.314 TEST_test_01[571:11303] PLAY
2013-05-08 10:26:30.314 TEST_test_01[571:11303] 注意先後順序,結果是:3
2013-05-08 10:26:30.314 TEST_test_01[571:11303] 這是第一個Block.
2013-05-08 10:26:30.315 TEST_test_01[571:11303] 這是第二個Block.
2013-05-08 10:26:30.315 TEST_test_01[571:11303] 這是第三個Block.
2013-05-08 10:26:30.316 TEST_test_01[571:11303] 多麼神奇的Block.result=100.
2013-05-08 10:26:47.200 TEST_test_01[571:11303] FUCK U MAN.
2013-05-08 10:26:47.200 TEST_test_01[571:11303] 注意調用的順序
2013-05-08 10:26:47.201 TEST_test_01[571:11303] ++++++++++
2013-05-08 10:26:47.201 TEST_test_01[571:11303] _______result:99
2013-05-08 10:26:47.201 TEST_test_01[571:11303] this is block_3.