day13 block
============
C函数指针回顾
#import <Foundation/Foundation.h>
int add(int a, int b) {
return a + b;
}
int sub(int a, int b) {
return a - b;
}
int calculation(int a, int b, int (*p)(int, int)) {
return p(a, b);
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
int a = 10, b = 5;
int (*pAdd)(int, int) = add;
int (*pSub)(int, int) = sub;
int value = pAdd(a, b);
printf("%d\n", value);
value = pSub(a, b);
printf("%d\n", value);
printf("%d\n", calculation(a, b, add));
printf("%d\n", calculation(a, b, sub));
}
return 0;
}
——————————————
block 代码块 无名的代码块/匿名代码块
block 代码块 和c的函数很像就是没有名字
void (^myBlock)(void) = ^void(void) {
NSLog(@"我是block");
};
myBlock();
void (^myBlock2)(void) = ^(void) {
NSLog(@"我是block 2");
};
myBlock2();
void (^myBlock3)() = ^{
NSLog(@"我是block 3");
};
myBlock3();
^(void){ // 和^void(void)等价
NSLog(@"我是block 4");
}();
——————————————
带参的block
int (^sum)(int, int) = ^int(int a, int b) {
return a+b;
};
NSLog(@"%d", sum(2, 5));
double (^maxValue)(double, double) = ^(double a, double b) {
return a > b ? a : b;
};
NSLog(@"%lf", maxValue(2.3, 5.6));
——————————————
block typedef
typedef void (^BlockType1)(void);
typedef double (^BlockType2)(double, double);
BlockType1 block1 = ^{
NSLog(@"block 1");
};
block1();
BlockType2 block2 = ^(double a, double b) {
return a + b;
};
NSLog(@"%lf", block2(1.2, 1.8));
#import "BlockTest.h"
typedef void (^BlockType1)(void);
typedef double (^BlockType2)(double a, double b);
@implementation BlockTest
- (void)test {
[self runBlock:^{
NSLog(@"run block");
}];
[self runBlock:^{
NSLog(@"run block again");
}];
[self runBlock2:^double(double a, double b) {
return a + b;
}];
[self runBlock2:^double(double a, double b) {
return a > b ? a : b;
}];
}
- (void)runBlock:(BlockType1)block {
block();
}
- (void)runBlock2:(BlockType2)block {
NSLog(@"%lf", block(1.2, 2.8));
}
@end
两个一起来:
runBlock1:(BlockType1)block1 block2:(BlockType2)block2
——————————————
// 官方一些常用block
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"oc", @"ui", @"iOS", nil];
// 升序排序
[arr sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2] > 0;
}];
NSLog(@"%@", arr);
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//obj 数组元素
//idx 数组索引
NSLog(@"obj: %@ idx: %ld", obj, idx);
//当 idx == 1 的时候中途结束循环 不能用break 那么可以修改*stop = YES;
if (idx == 1) {
*stop = YES;
}
}];
——————————————
#import "Test.h"
typedef void (^BlockType1)(void);
typedef double (^BlockType2)(double a, double b);
@implementation Test
// 如果block 内部要修改外面的局部变量,必须要在外面的变量前面加一个__block
// __block 加上之后 会告知编译器,在block 内部拷贝一个cnt, 这个cnt 和外面的cnt 值 是保持同步的
- (void)test {
__block int cnt = 0;
NSLog(@"%d", cnt);
/**
void (^myBlock)() = ^{
NSLog(@"%d", cnt);
};
[self runBlock1:myBlock];
*/
[self runBlock1:^{
cnt++; // cnt 默认在block 代码块只能读 如果要修改需要加__block
NSLog(@"block: %d", cnt);
}];
NSLog(@"block: %d", cnt);
// 当前类的成员变量(或者全局变量)可以在block内部修改
}
- (void)runBlock1:(BlockType1)block {
block();
}
- (void)runBlock2:(BlockType2)block {
NSLog(@"%lf", block(1, 2));
}
@end
——————————————
- (void)test {
// 在OC中block 代码块 可以认为是一个对象
// = 右侧的代码块 可以认为就是一个对象
void (^myBlock)(void) = ^{
NSLog(@"myBlock");
};
void (^newBlock)(void) = [myBlock copy];
newBlock();
[newBlock release];
void (^newBlock2)(void) = Block_copy(myBlock);
newBlock2();
Block_release(newBlock2);
// Block_release(myBlock);
}
——————————————
block传值,老板的员工干活,干完活之后要求老板发工资。
——————————————
作业:
写一个block传值demo, 司机开车,老板买东西
——————————————