響應(yīng)式編程
響應(yīng)式編程是一種面向數(shù)據(jù)流和變化傳播的編程范式。簡(jiǎn)單來說,就是變化的結(jié)果可以自動(dòng)的通過數(shù)據(jù)流傳播。以a = b
這個(gè)表達(dá)式為例错负,表示的意思是將b的值賦值給a,如果再次修改了b的值勇边,a的值并不會(huì)自動(dòng)修改犹撒。但是在響應(yīng)式編程框架中,可以將a和b進(jìn)行某種數(shù)據(jù)流上的綁定粒褒,改變了b的值识颊,就會(huì)自動(dòng)修改a的值,就像excel表格中的求和計(jì)算公式一樣怀浆,改變一個(gè)單元格數(shù)據(jù)谊囚,就會(huì)自動(dòng)改變總和。
KVO
KVO
的大致實(shí)現(xiàn)邏輯是這樣的执赡,當(dāng)觀察某對(duì)象 A 時(shí)镰踏,KVO
機(jī)制動(dòng)態(tài)創(chuàng)建一個(gè)對(duì)象A當(dāng)前類的子類,并為這個(gè)新的子類重寫了被觀察屬性 keyPath
的 setter
方法沙合。setter
方法隨后負(fù)責(zé)通知觀察對(duì)象屬性的改變狀況奠伪。
以Person
類為例,具體邏輯如下:
1.動(dòng)態(tài)創(chuàng)建NSKVONotifying_Person
,NSKVONotifying_Person
是Person子類
2.修改當(dāng)前對(duì)象的isa指針指向NSKVONotifying_Person
3.只要調(diào)用對(duì)象的set,就會(huì)調(diào)用NSKVONotifying_Person
的set
方法
4.重寫NSKVONotifying_Person
的set
方法首懈,在重寫的set方法里绊率,主要做了兩件事情,1.[super set:]
2.通知觀察者,告訴你屬性改變
自定義一下KVO的具體實(shí)現(xiàn):
@interface NSObject (hz_KVO)
- (void)hz_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context;
@end
@implementation NSObject (hz_KVO)
- (void)hz_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context{
//動(dòng)態(tài)生成一個(gè)類
NSString *oldClassName = NSStringFromClass([self class]);
NSString *newName = [@"NShz_" stringByAppendingString:oldClassName];
const char * newClassName = [newName UTF8String];
//定義 創(chuàng)建一個(gè)類的class
Class hzClass = objc_allocateClassPair([self class], newClassName, 0);
class_addMethod(hzClass, @selector(setName:), (IMP)setName, "v@:@");
//注冊(cè)
objc_registerClassPair(hzClass);
//指向 修改isa,本質(zhì)就是改變當(dāng)前對(duì)象的類名
object_setClass(self, hzClass);
//綁定 保存觀察者對(duì)象
objc_setAssociatedObject(self, (__bridge const void *)@"123", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
void setName(id self,SEL _cmd,NSString * newName){
//保存子類類型
id class = [self class];
//改變self 的isa指針
object_setClass(self, class_getSuperclass([self class]));
//調(diào)用父類的set方法
objc_msgSend(self, @selector(setName:),newName);
//拿到觀察者
id observer = objc_getAssociatedObject(self, (__bridge const void *)@"123");
//通知觀察者
objc_msgSend(observer, @selector(observeValueForKeyPath:ofObject:change:context:),self,@"name",@{@"new":newName},nil);
//改回子類類型
object_setClass(self, class);
}
調(diào)用:
@interface ViewController ()
@property(nonatomic,strong)Dog * dog;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
Dog * dog = [[Dog alloc]init];
[dog hz_addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew context:nil];
_dog = dog;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
NSLog(@"%@===>%@",change,_dog.name);
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
static int i = 0;
i++;
_dog.name = [NSString stringWithFormat:@"%d",i];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end