由于最近看到现在的招聘简历一言不合就来一波熟悉ReactiveCocoa函数链式编程,为了紧跟时代,于是作为菜鸟的我也来研究一波rac, 何为ReactiveCocoa(以下简称rac)? rac 由GitHub开源的一个应用于iOS和OS X开发的新框架(由19个大牛编写而来)。rac具有函数式编程和响应式编程的特性, 作为ios程序员,为何要使用rac呢? 请看下图:
没看懂?or不想看?好吧,作为只会Ctr C +V的我来说,敲代码就是一把梭,我可以说学习rac就是为了装一波X么?会不会被那打死??🤣
言归正传,当pod下来rac的代码下来试一波,你就会发现,rac用着巨爽,主要是为ios程序员省掉了很多代码量,可以让代码更加优雅,比如在OC里面的的常见事件处理,在rac里面的代码量就很少了,请看下图:
从上图我们可以看到rac对UI控件、代理、等等做了支持,具体的不多说,看下面的代码:
//taget-Action
[[self.touch rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
NSLog(@"x = %@",x);
_alertView = [[UIAlertView alloc] initWithTitle:@"xxx" message:@"xxx" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"other", nil];
[_alertView show];
/*
//对delegate的支持
[[self rac_signalForSelector:@selector(alertView:clickedButtonAtIndex:) fromProtocol:@protocol(UIAlertViewDelegate)] subscribeNext:^(RACTuple *tupe) {
NSLog(@"first = %@",tupe.first);
NSLog(@"second = %@",tupe.second);
}];
*/
@weakify(self);
[[_alertView rac_buttonClickedSignal] subscribeNext:^(id x) {
@strongify(self);
//rac里面是使用weakify和stringify来避免循环引用
NSLog(@"x = %@",x);
NSInteger index = [x integerValue];
NSLog(@"你点击的是第%zd个按钮",index);
if (index == 1) {
NextVC *next = [NextVC new];
next.delegate = self;
[self.navigationController pushViewController:next animated:YES];
///rac对代理支持,但是这里值得注意的是,rac的代理目前只能支持返回值是void的代理,带有返回值的代理是不支持的
[[self rac_signalForSelector:@selector(nextVC:value:) fromProtocol:@protocol(NextVCDelegate)] subscribeNext:^(RACTuple * tupe) {
NSLog(@"value = %@",tupe);
}];
}
}];
//dismiss
[[_alertView rac_willDismissSignal] subscribeNext:^(id x) {
NSLog(@"alertView will dismiss \n x = %@",x);
}];
}];
//通知
[[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIApplicationDidEnterBackgroundNotification object:nil] subscribeNext:^(NSNotification *noti) {
NSLog(@"notification = %@",noti.name);
}];
//KVO
[RACObserve(self.touch, frame) subscribeNext:^(id x) {
// NSLog(@"frame = %@",[x CGRectValue]);
}];
self.view.userInteractionEnabled = YES;
//对tap的监听
UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
[[tap rac_gestureSignal] subscribeNext:^(id x) {
NSLog(@"tapview = %@",((UITapGestureRecognizer *)x).view);
}];
[self.view addGestureRecognizer:tap];
//信号量
RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
//发送next信号
[subscriber sendNext:@"Chan"];
//发送完成
[subscriber sendCompleted];
//释放操作
return [RACDisposable disposableWithBlock:^{
}];
}];
//订阅
[signal subscribeNext:^(id x) {
NSLog(@"x = %@",x);
}];
//订阅错误
[signal doError:^(NSError *error) {
NSLog(@"subcribe error = %@",error.localizedDescription);
}];
//订阅完成
[signal doCompleted:^{
}];
_pickerController = [UIImagePickerController new];
_pickerController.delegate = self;
_pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_pickerController.allowsEditing = NO;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
self.touch.frame = CGRectMake(100, 100, 120, 50);
[self presentViewController:_pickerController animated:YES completion:nil];
[[self rac_signalForSelector:@selector(imagePickerController:didFinishPickingMediaWithInfo:) fromProtocol:@protocol(UIImagePickerControllerDelegate)] subscribeNext:^(RACTuple *tupe) {
// tupe里面存着delegate里面的参数
NSLog(@"first = %@\n second = %@",tupe.first,tupe.second);
}];
}