鏈?zhǔn)骄幊趟枷?
#1>一般開發(fā)習(xí)慣把事情封裝到一個(gè)方法中沛豌;鏈?zhǔn)骄幊趟枷胧前岩龅氖虑榉庋b到block中测蹲,給外部提供一個(gè)返回這個(gè)block的方法
#2>鏈?zhǔn)骄幊趟枷敕椒ㄌ攸c(diǎn):方法的返回值必須是block,block的參數(shù)是需要操作的內(nèi)容紫新,block的返回值是返回這個(gè)block的方法的調(diào)用者
1.設(shè)計(jì)成類方法-不可行錯(cuò)誤的實(shí)現(xiàn)方式
在開發(fā)中有了一個(gè)想法就動(dòng)手寫代碼,在實(shí)現(xiàn)的過程中,逐步完善思路.
1.1不知道方法的返回值 就返回void
add方法無法連續(xù)調(diào)用,所以add方法必須返回
CalculateMaker
類
1.2方法返回值是類本身
需要把累加的結(jié)果保存起來,但是類方法不能使用屬性项炼,使用static 靜態(tài)全局變量
2.設(shè)計(jì)成對(duì)象方法
add方法要連續(xù)調(diào)用,add方法就要返回CalculateMaker類的對(duì)象實(shí)例
CalculateMaker.h
#import <Foundation/Foundation.h>
@interface CalculateMaker : NSObject
@property (nonatomic,assign) int result;
- (instancetype)add:(int)num;
@end
CalculateMaker.m
#import "CalculateMaker.h"
@implementation CalculateMaker
- (instancetype)add:(int)num{
_result += num;
return self;
}
@end
ViewController.m
#import "ViewController.h"
#import "CalculateMaker.h"
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
CalculateMaker *maker = [[CalculateMaker alloc] init];
int result = [[[[maker add:10] add:10] add:10] result];
NSLog(@"result = %d",result);
}
// 2016-08-14 00:14:30.787 CalculateMaker[8230:1471099] result = 30
@end
3.鏈?zhǔn)骄幊痰目勺x性 返回值是block類型
maker.add(10).add(20).add(30).add(40)
只有返回值是block才能實(shí)現(xiàn)該效果
3.1add方法返回值是block,block類型無參數(shù)無返回值
//返回值block,block類型無參數(shù)無返回值
- (void(^)())add;
- (void (^)())add{
return ^{
NSLog(@"返回一個(gè)block,該block無參數(shù)無返回值");
};
}
CalculateMaker *maker = [[CalculateMaker alloc] init];
maker.add();
//2016-08-14 00:58:01.473 CalculateMaker[10247:1491527] 返回一個(gè)block,該block無參數(shù)無返回值
CalculateMaker
類的實(shí)例有一個(gè)add
方法,調(diào)用add方法返回值是一個(gè)block,外部拿到這個(gè)block可以block();
執(zhí)行里面封裝的代碼
計(jì)算的操作在block中完成,外部不需要知道block的實(shí)現(xiàn)細(xì)節(jié),外部只是調(diào)用執(zhí)行該block即可
3.2add方法返回值是block,block類型有參數(shù)(int)無返回值
- (void(^)(int num))add;
- (void (^)(int))add{
return ^(int num){
NSLog(@"add方法返回一個(gè)block,該block有int參數(shù)無返回值");
};
}
CalculateMaker *maker = [[CalculateMaker alloc] init];
maker.add(10);
//2016-08-14 01:06:07.349 CalculateMaker[11045:1497617] add方法返回一個(gè)block,該block有int參數(shù)無返回值
鏈?zhǔn)骄幊趟枷?/code>:把業(yè)務(wù)邏輯的實(shí)現(xiàn)封裝在block担平,把block返回到外部執(zhí)行
如何連續(xù)執(zhí)行add方法,讓這個(gè)block執(zhí)行完畢返回CalculateMaker類的對(duì)象實(shí)例
即可
3.3add方法返回值是block,block類型有參數(shù)(int)有返回值(類的對(duì)象實(shí)例)
- (instancetype (^)(int num))add;
- (instancetype (^)(int))add{
return ^(int num){
NSLog(@"add方法返回一個(gè)block,該block有int參數(shù)有返回值是類的對(duì)象實(shí)例");
return self;
};
}
CalculateMaker *maker = [[CalculateMaker alloc] init];
maker.add(10).add(20);
- (CalculateMaker* (^)(int num))add;
- (CalculateMaker* (^)(int))add{
return ^(int num){
NSLog(@"add方法返回一個(gè)block,該block有int參數(shù)有返回值是類的對(duì)象實(shí)例");
return self;
};
}
CalculateMaker *maker = [[CalculateMaker alloc] init];
maker.add(10).add(20);
//2016-08-14 01:21:23.542 CalculateMaker[12562:1513205] add方法返回一個(gè)block,該block有int參數(shù)有返回值是類的對(duì)象實(shí)例
//2016-08-14 01:21:23.543 CalculateMaker[12562:1513205] add方法返回一個(gè)block,該block有int參數(shù)有返回值是類的對(duì)象實(shí)例
add方法返回對(duì)象示绊,就可以繼續(xù)調(diào)用對(duì)象的add方法了
CalculateMaker.h
#import <Foundation/Foundation.h>
@interface CalculateMaker : NSObject
@property (nonatomic,assign) int result;
- (CalculateMaker* (^)(int num))add;
@end
CalculateMaker.m
#import "CalculateMaker.h"
@implementation CalculateMaker
- (CalculateMaker* (^)(int))add{
return ^(int num){
_result += num;
return self;
};
}
@end
ViewController.m
#import "ViewController.h"
#import "CalculateMaker.h"
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
CalculateMaker *maker = [[CalculateMaker alloc] init];
int result = maker.add(10).add(20).result;
NSLog(@"result = %d",result);
}
// 2016-08-14 01:34:31.024 CalculateMaker[13848:1526899] result = 30
@end
4.所有的NSObject都能計(jì)算 分類
讓所有的OC的類都能計(jì)算,給所有的類分類出一個(gè)方法暂论,把實(shí)現(xiàn)計(jì)算的操作聚合在一個(gè)block代碼塊中
最終實(shí)現(xiàn)代碼如下:
CalculateMaker.h
//
// CalculateMaker.h
// CalculateMaker
//
// Created by zhaoyingxin on 16/8/13.
// Copyright ? 2016年 zhaoyingxin@aliyun.com. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CalculateMaker : NSObject
@property (nonatomic,assign) int result;
- (CalculateMaker* (^)(int num))add;
@end
CalculateMaker.m
#import "CalculateMaker.h"
@implementation CalculateMaker
- (CalculateMaker* (^)(int))add{
return ^(int num){
_result += num;
return self;
};
}
@end
NSObject+Calculate.h
#import <Foundation/Foundation.h>
#import "CalculateMaker.h"
@interface NSObject (Calculate)
+ (int)zyx_makeCalculate:(void (^) (CalculateMaker *))block;
@end
NSObject+Calculate.m
#import "NSObject+Calculate.h"
@implementation NSObject (Calculate)
+(int)zyx_makeCalculate:(void (^)(CalculateMaker *))block{
// 計(jì)算制造者
CalculateMaker *maker = [[CalculateMaker alloc] init];
// 執(zhí)行計(jì)算
block(maker);
// 返回計(jì)算結(jié)果
return maker.result;
}
@end
ViewController.m
#import "ViewController.h"
#import "NSObject+Calculate.h"
@implementation ViewController
- (void)viewDidLoad{
[super viewDidLoad];
int result = [NSObject zyx_makeCalculate:^(CalculateMaker *maker) {
maker.add(1).add(2).add(3);
maker.add(4).add(5).add(6);
}];
NSLog(@"result = %d",result);
}
//2016-08-14 02:35:26.927 CalculateMaker[17132:1554227] result = 21
@end