标签:
前些天学到了Block,对于Block有了一些大致的认识
Block是什么?
我认为是一些行为的封装
举个例子 要将两个参数进行加减乘除
创建4个Block实现价钱乘除四个操作即可
用一个方法 传两个参数和一个block 返回就是block对这两个参数的操作
代码演示
1 #import <Foundation/Foundation.h>
2 #import "Calc.h"
3 int main(int argc, const char * argv[]) {
4 @autoreleasepool {
5 // typedef
6 Calc * calc = [[Calc alloc] init];
7 // NSLog(@"%d",[calc calculateWithNumber1:3 andNumber2:5 withCalBlock:^int(int a, int b) {
8 // return a-b;
9 // }]);
10
11 NSLog(@"%d",[calc calculateWithNumber1:2 andNumber2:5 withCalBlock:^int(int q, int w) {
12 return q*w;
13 }]);
14 // NSLog(@"%d",[calc calculateWithNumber1:3 andNumber2:6 withCalcBlock:^int(int a, int b) {
15 // return a*b;
16 // }]);
17 }
18 return 0;
19 }
1 #import <Foundation/Foundation.h>
2
3 typedef int (^CalcBlock) (int,int); // 定义一个block类型,类型的名称为CalcBlock
4 @interface Calc : NSObject
5 - (int)calculateWithNumber1:(int)num1 andNumber2:(int)num2 withCalBlock:(int (^)(int,int))calBlock;
6 - (int)calculateWithNumber1:(int)num1 andNumber2:(int)num2 withCalcBlock:(CalcBlock)calcBlock;
7 - (int)calculateWithNumber1:(int)num1 andNumber2:(int)num2 withSel:(SEL)sel;
8
9 @end
1 #import "Calc.h"
2
3 @implementation Calc
4 - (int)calculateWithNumber1:(int)num1 andNumber2:(int)num2 withCalBlock:(int (^)(int,int))calBlock {
5 return calBlock(num1,num2);
6 }
7
8 - (int)calculateWithNumber1:(int)num1 andNumber2:(int)num2 withCalcBlock:(CalcBlock)calcBlock {
9 return calcBlock(num1,num2);
10 }
11
12 @end
Block语法
返回值(^Block名称)(参数列表) = ^返回值(参数列表){};
使用Blcok实现数组排序 根据Block返回值的不同来实现从大到小或从小到大的排序
以下代码 在循环遍历事 可以用Block来控制数组元素交换的条件 从而达到排序效果
1 #import <Foundation/Foundation.h>
2 #import "NSArray+Extention.h"
3 int main(int argc, const char * argv[]) {
4 @autoreleasepool {
5 NSArray * array = @[@"b",@"f",@"d",@"a",@"c"];
6 NSArray * array2 = [array sortedArrayByBlock:^BOOL(id obj1, id obj2) {
7 return [obj1 isGreaterThan:obj2];
8 }];
9 [array2 show];
10
11
12 }
13 return 0;
14 }
1 #import <Foundation/Foundation.h>
2
3 //扩展?
4 @interface NSArray (Extention)
5 - (void)show;
6 - (NSArray *)sortedArrayByBlock:(BOOL(^)(id,id))cmp;
7 @end
1 #import "NSArray+Extention.h"
2
3 @implementation NSArray (Extention)
4 - (void)show {
5 for (id obj in self) {
6 NSLog(@"%@",obj);
7 }
8 }
9
10 - (NSArray *)sortedArrayByBlock:(BOOL (^)(id, id))cmp {
11 NSMutableArray * muarray = [NSMutableArray arrayWithArray:self];
12 for (int i = 0; i<self.count - 1; i++) {
13 for (int j = 0; j< self.count-1-i; j++) {
14 //引入Block方法
15 if (cmp(muarray[j],muarray[j+1])) {
16 [muarray exchangeObjectAtIndex:j withObjectAtIndex:j+1];
17 }
18 }
19 }
20 return [muarray copy];
21 }
22
23
24 @end
然后是老师的一个例子 用Block实现Boss让Worker买电脑
虽然我能看懂代码 但实在不理解 这样做的意义。。 Block的意义在于把Boss的电脑个数增加
附上代码
1 #import <Foundation/Foundation.h>
2 #import "Boss.h"
3 #import "Worker.h"
4
5 int main(int argc, const char * argv[]) {
6 @autoreleasepool {
7 Worker * worker = [[Worker alloc] init];
8 Boss * boss = [[Boss alloc] init];
9 [boss showMacbooks];
10 [worker buyMacbookWithNum:12 withSendFinishedBlock:^(int num) {
11 boss.numOfMacbooks += num;
12 }];
13 [boss showMacbooks];
14 }
15 return 0;
16 }
1 #import <Foundation/Foundation.h>
2 #import "Worker.h"
3 @interface Boss : NSObject
4 @property (nonatomic,assign) int numOfMacbooks;
5 - (void)showMacbooks;
6 @end
1 #import "Boss.h"
2
3 @implementation Boss
4 - (void)showMacbooks {
5 NSLog(@"老板现在有Macbook%d台",_numOfMacbooks);
6 }
7
8 @end
1 #import <Foundation/Foundation.h>
2
3 @interface Worker : NSObject
4 - (void)buyMacbookWithNum:(int)num withSendFinishedBlock:(void(^)(int))sentBlock;
5 @end
1 #import "Worker.h"
2
3 @implementation Worker
4 - (void)buyMacbookWithNum:(int)num withSendFinishedBlock:(void(^)(int))sentBlock{
5 NSLog(@"买了%d台Macbook",num);
6 NSLog(@"给老板%d台",num);
7 sentBlock(num);
8 }
9 @end
标签:
原文地址:http://www.cnblogs.com/gwkiOS/p/4967579.html