码迷,mamicode.com
首页 > 其他好文 > 详细

关于Block初识与自己的认识

时间:2015-11-15 23:20:29      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:

前些天学到了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 }
main.m
技术分享
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
Calc.h
技术分享
 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
Calc.m

 

 

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 }
main.m
技术分享
1 #import <Foundation/Foundation.h>
2 
3 //扩展?
4 @interface NSArray (Extention)
5 - (void)show;
6 - (NSArray *)sortedArrayByBlock:(BOOL(^)(id,id))cmp;
7 @end
NSArray+Extention.h
技术分享
 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
NSArray+Extention.m

 

 

然后是老师的一个例子  用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 }
main.m
技术分享
1 #import <Foundation/Foundation.h>
2 #import "Worker.h"
3 @interface Boss : NSObject
4 @property (nonatomic,assign) int numOfMacbooks;
5 - (void)showMacbooks;
6 @end
Boss.h
技术分享
1 #import "Boss.h"
2 
3 @implementation Boss
4 - (void)showMacbooks {
5     NSLog(@"老板现在有Macbook%d台",_numOfMacbooks);
6 }
7 
8 @end
Boss.m
技术分享
1 #import <Foundation/Foundation.h>
2 
3 @interface Worker : NSObject
4 - (void)buyMacbookWithNum:(int)num withSendFinishedBlock:(void(^)(int))sentBlock;
5 @end
Worker.h
技术分享
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
Worker.m

 

关于Block初识与自己的认识

标签:

原文地址:http://www.cnblogs.com/gwkiOS/p/4967579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!