1. 鏈?zhǔn)骄幊趟枷?/h3>
鏈?zhǔn)骄幊趟枷氲暮诵木褪?strong>方法的返回值必須是Block耸峭,Masonry是鏈?zhǔn)骄幊趟枷霊?yīng)用的代表。
1.1 Masonry應(yīng)用鏈?zhǔn)骄幊趟枷朐斀?/h3>
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
/**
給控件設(shè)置布局,把控件的所有約束保存到約束制造者中.
1.創(chuàng)建一個約束制造者
2.調(diào)用block(maker),把所有的控件的約束全部保存到約束制造者
3.[constraintMaker install]:遍歷約束制造者的所有約束給控件添加約束
@param make 約束制造者
@return MASConstraint
*/
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(@10);
make.right.bottom.equalTo(@(-10));
}];
1.2 鏈?zhǔn)骄幊趟枷霊?yīng)用---計算器
@interface CaculatorMaker : NSObject
@property (nonatomic, assign) int result;
- (instancetype)add:(int)num;
- (CaculatorMaker *(^)(int num))add;
- (CaculatorMaker *(^)(int))sub;
- (CaculatorMaker *(^)(int num))multy;
- (CaculatorMaker *(^)(int))divide;
@end
@implementation CaculatorMaker
- (instancetype)add:(int)num
{
_result += num;
return self;
}
- (CaculatorMaker * (^)(int num))add
{
return ^(int num){
_result += num;
return self;
};
}
@end
#import <Foundation/Foundation.h>
#import "CaculatorMaker.h"
@interface NSObject (Caculator)
// 以后計算都使用這個方法,一調(diào)用這個方法就返回結(jié)果.
+ (int)makeCaculator:(void(^)(CaculatorMaker *))block;
@end
#import "NSObject+Caculator.h"
#import "CaculatorMaker.h"
@implementation NSObject (Caculator)
+ (int)makeCaculator:(void (^)(CaculatorMaker *))block
{
// 創(chuàng)建計算制造者
CaculatorMaker *maker = [[CaculatorMaker alloc] init];
// 計算
block(maker);
return maker.result;
}
@end
// 1.創(chuàng)建計算制造者
CaculatorMaker *maker = [[CaculatorMaker alloc] init];
// 提供一個沒參數(shù)的add方法,返回值block
int reslut1 = [[[[[maker add:10] add:20] add:30] add:40] result];
// block:使代碼高聚合
int result2 = [NSObject makeCaculator:^(CaculatorMaker *maker) {
// 把所有的計算代碼封裝到這里
maker.add(10).add(20);
maker.multy(3);
}];
NSLog(@"%d",result2);
UIView *redView = [[UIView alloc] init];
redView.backgroundColor = [UIColor redColor];
[self.view addSubview:redView];
/**
給控件設(shè)置布局,把控件的所有約束保存到約束制造者中.
1.創(chuàng)建一個約束制造者
2.調(diào)用block(maker),把所有的控件的約束全部保存到約束制造者
3.[constraintMaker install]:遍歷約束制造者的所有約束給控件添加約束
@param make 約束制造者
@return MASConstraint
*/
[redView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.equalTo(@10);
make.right.bottom.equalTo(@(-10));
}];
@interface CaculatorMaker : NSObject
@property (nonatomic, assign) int result;
- (instancetype)add:(int)num;
- (CaculatorMaker *(^)(int num))add;
- (CaculatorMaker *(^)(int))sub;
- (CaculatorMaker *(^)(int num))multy;
- (CaculatorMaker *(^)(int))divide;
@end
@implementation CaculatorMaker
- (instancetype)add:(int)num
{
_result += num;
return self;
}
- (CaculatorMaker * (^)(int num))add
{
return ^(int num){
_result += num;
return self;
};
}
@end
#import <Foundation/Foundation.h>
#import "CaculatorMaker.h"
@interface NSObject (Caculator)
// 以后計算都使用這個方法,一調(diào)用這個方法就返回結(jié)果.
+ (int)makeCaculator:(void(^)(CaculatorMaker *))block;
@end
#import "NSObject+Caculator.h"
#import "CaculatorMaker.h"
@implementation NSObject (Caculator)
+ (int)makeCaculator:(void (^)(CaculatorMaker *))block
{
// 創(chuàng)建計算制造者
CaculatorMaker *maker = [[CaculatorMaker alloc] init];
// 計算
block(maker);
return maker.result;
}
@end
// 1.創(chuàng)建計算制造者
CaculatorMaker *maker = [[CaculatorMaker alloc] init];
// 提供一個沒參數(shù)的add方法,返回值block
int reslut1 = [[[[[maker add:10] add:20] add:30] add:40] result];
// block:使代碼高聚合
int result2 = [NSObject makeCaculator:^(CaculatorMaker *maker) {
// 把所有的計算代碼封裝到這里
maker.add(10).add(20);
maker.multy(3);
}];
NSLog(@"%d",result2);
之前開發(fā)中比較習(xí)慣把事情封裝到一個方法中,鏈?zhǔn)骄幊趟枷?把要做的事情封裝到block,給外界提供一個返回這個Block的方法淋纲。
鏈?zhǔn)骄幊趟枷敕椒ㄌ攸c:
方法返回值必須是block
block參數(shù):放需要操作的內(nèi)容
block返回值:方法調(diào)用者
2. 響應(yīng)式編程思想
不需要考慮調(diào)用順序,只需要知道考慮結(jié)果院究,類似于蝴蝶效應(yīng)洽瞬,產(chǎn)生一個事件,會影響很多東西业汰,這些事件像流一樣的傳播出去伙窃,然后影響結(jié)果,借用面向?qū)ο蟮囊痪湓捬幔f物皆是流为障。代表有KVO的運用。
模仿系統(tǒng)提供的KVO:
@interface Person : NSObject
{
@public
int _age;
}
@property (nonatomic, assign) int age;
@end
@implementation Person
@end
@interface LLKVONotifying_Person : Person
@end
#import "LLKVONotifying_Person.h"
#import <objc/runtime.h>
@implementation LLKVONotifying_Person
- (void)setAge:(int)age
{
[super setAge:age];
// 通知觀察者,屬性改變
id observer = objc_getAssociatedObject(self, @"observer");
[observer observeValueForKeyPath:@"age" ofObject:self change:nil context:nil];
}
@end
@interface NSObject (KVO)
- (void)LL_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(nullable void *)context;
@end
#import "NSObject+KVO.h"
#import "LLKVONotifying_Person.h"
#import <objc/runtime.h>
@implementation NSObject (KVO)
- (void)LL_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
{
// 1.動態(tài)創(chuàng)建NSKVONotifying_Person,NSKVONotifying_Person是Person子類,做KVO
// 2.修改當(dāng)前對象的isa指針->NSKVONotifying_Person
// 3.只要調(diào)用對象的set,就會調(diào)用NSKVONotifying_Person的set方法
// 4.重寫NSKVONotifying_Person的set方法,1.[super set:] 2.通知觀察者,告訴你屬性改變
// 修改isa,本質(zhì)就是改變當(dāng)前對象的類名
object_setClass(self, [LLKVONotifying_Person class]);
// 把觀察者保存到當(dāng)前對象里
// 添加關(guān)聯(lián)
// id object:給哪個對象添加關(guān)聯(lián)屬性
// key:屬性名
// value:關(guān)聯(lián)值
objc_setAssociatedObject(self, @"observer", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end
@interface ViewController ()
@property (nonatomic, strong) Person *p;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Person *p = [[Person alloc] init];
_p = p;
// 添加觀察者
//[p addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew context:nil];
[p LL_addObserver:self forKeyPath:@"age" options:NSKeyValueObservingOptionNew context:nil];
}
// 監(jiān)聽的屬性只要一改變就調(diào)用
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
NSLog(@"%d",_p.age);
}
KVO底層實現(xiàn):
1.動態(tài)創(chuàng)建NSKVONotifying_Person,NSKVONotifying_Person是Person子類,做KVO
2.修改當(dāng)前對象的isa指針->NSKVONotifying_Person
3.只要調(diào)用對象的set,就會調(diào)用NSKVONotifying_Person的set方法
4.重寫NSKVONotifying_Person的set方法,1.[super set:] 2.通知觀察者,告訴你屬性改變
KVO的實質(zhì)就是去判斷有沒有調(diào)用一個對象的set方法放祟。
3. 函數(shù)式編程思想
其實就是把操作盡量寫成一系列嵌套的函數(shù)或者方法調(diào)用鳍怨。
特點:每個方法必須有返回值(本身對象),把函數(shù)或者Block當(dāng)做參數(shù),block參數(shù)(需要操作的值)block返回值(操作結(jié)果)
利用函數(shù)式編程寫一個加法計算器,并且加法計算器自帶判斷是否等于某個值。
@interface Caculator : NSObject
@property (nonatomic, assign) int result;
@property (nonatomic, assign) BOOL isEqule;
- (instancetype)add:(int(^)(int result))block;
- (instancetype)equle:(BOOL(^)(int result))block;
@end
@implementation Caculator
- (instancetype)add:(int (^)(int result))block
{
_result = block(_result);
return self;
}
- (instancetype)equle:(BOOL (^)(int))block
{
_isEqule = block(_result);
return self;
}
@end
Caculator *caculator = [[Caculator alloc] init];
BOOL isEqule = [[[caculator add:^(int result){
// 把計算的事情寫到Block
result += 10;
result += 20;
result += 30;
result += 40;
return result;
}] equle:^BOOL(int result) {
return result == 100;
}] isEqule];
函數(shù)式編程的代表是:ReactiveCocoa跪妥,在下一篇中我們會詳細討論ReactiveCocoa鞋喇。