iOS開發(fā)之ReactiveCocoa入門基礎(chǔ)

由于最近看到現(xiàn)在的招聘簡(jiǎn)歷一言不合就來一波熟悉ReactiveCocoa函數(shù)鏈?zhǔn)骄幊?,為了緊跟時(shí)代,于是作為菜鳥的我也來研究一波rac, 何為ReactiveCocoa(以下簡(jiǎn)稱rac)? rac 由GitHub開源的一個(gè)應(yīng)用于iOS和OS X開發(fā)的新框架(由19個(gè)大牛編寫而來)。rac具有函數(shù)式編程響應(yīng)式編程的特性, 作為ios程序員,為何要使用rac呢? 請(qǐng)看下圖:

RAC.png

沒看懂?or不想看?好吧,作為只會(huì)Ctr C +V的我來說,敲代碼就是一把梭,我可以說學(xué)習(xí)rac就是為了裝一波X么?會(huì)不會(huì)被那打死????
言歸正傳,當(dāng)pod下來rac的代碼下來試一波,你就會(huì)發(fā)現(xiàn),rac用著巨爽,主要是為ios程序員省掉了很多代碼量,可以讓代碼更加優(yōu)雅,比如在OC里面的的常見事件處理,在rac里面的代碼量就很少了,請(qǐng)看下圖:
rac->structure.png

rac->UI.png

從上圖我們可以看到rac對(duì)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];
        /*
         //對(duì)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來避免循環(huán)引用
            NSLog(@"x = %@",x);
            NSInteger index =  [x integerValue];
            NSLog(@"你點(diǎn)擊的是第%zd個(gè)按鈕",index);
            if (index == 1) {
                NextVC *next = [NextVC new];
                next.delegate = self;
                [self.navigationController pushViewController:next animated:YES];
                
                ///rac對(duì)代理支持,但是這里值得注意的是,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;
    //對(duì)tap的監(jiān)聽
    UITapGestureRecognizer *tap = [UITapGestureRecognizer new];
    [[tap rac_gestureSignal] subscribeNext:^(id x) {
        NSLog(@"tapview = %@",((UITapGestureRecognizer *)x).view);
    }];
    [self.view addGestureRecognizer:tap];
    
    //信號(hào)量
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        //發(fā)送next信號(hào)
        [subscriber sendNext:@"Chan"];
        //發(fā)送完成
        [subscriber sendCompleted];
        //釋放操作
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
    
    //訂閱
    [signal subscribeNext:^(id x) {
        NSLog(@"x = %@",x);
    }];
    //訂閱錯(cuò)誤
    [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里面的參數(shù)
        NSLog(@"first = %@\n second = %@",tupe.first,tupe.second);
    }];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容