目前大量的ios應用程序在開發的過程當中,是經過用戶響應來改變應用程序狀態的。咱們在這樣處理事件時,代碼每每會變得很是複雜,須要用到大量的回調和狀態變量來完成回調函數的功能。ios
其實像 UI回調 、 網絡響應 、 KVO通知 等等,會有不少的共同之處,都須要回調函數進行處理功能。git
那麼RACSingal統一了這些不一樣的API,方便他們可以組合在一塊兒完成複雜的功能。github
咱們先看一下下邊的代碼:網絡
@implementation ViewController { MethodManager *manager; } - (void)awakeFromNib { manager = [MethodManager sharedManager]; } - (void)viewDidLoad { [super viewDidLoad]; [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(loggedOut:) name:@"loggedOut" object:manager]; [self.usernameTextField addTarget:self action:@selector(textDelegate) forControlEvents:UIControlEventEditingChanged]; [self.passwordTextField addTarget:self action:@selector(textDelegate) forControlEvents:UIControlEventEditingChanged]; [self.logInButton addTarget:self action:@selector(didClickLogin:) forControlEvents:UIControlEventTouchUpInside]; } - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; } - (void)textDelegate { BOOL textFieldsNonEmpty = self.usernameTextField.text.length > 0 && self.passwordTextField.text.length > 0; BOOL readyToLogIn = !LoginManager.sharedManager.isLoggingIn && !self.loggedIn; self.logInButton.enabled = textFieldsNonEmpty && readyToLogIn; } - (IBAction)didClickLogin:(UIButton *)sender { [manager logInWithUsername:self.usernameTextField.text password:self.passwordTextField.text success:^{ self.loggedIn = YES; } failure:^(NSError *error) { [self presentError:error]; }]; } - (void)loggedOut:(NSNotification *)notification { self.loggedIn = NO; } @end
若是是RAC代碼的話,咱們就可使用以下代碼:框架
@implementation ViewController { MethodManager *manager; } - (void)awakeFromNib { manager = [MethodManager sharedManager]; } - (void)viewDidLoad { [super viewDidLoad]; RAC(self.signButton, enabled) = [RACSignal combineLatest:@[ self.password.rac_textSignal, self.confirm.rac_textSignal, RACObserve(manager, signing), RACObserve(self, signing) ] reduce:^(NSString *username, NSString *password, NSNumber *loggingIn, NSNumber *loggedIn) { return @(username.length > 0 && password.length > 0 && !signing.boolValue && !signing.boolValue); }]; [[self.signButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *sender) { RACSignal *signingSignal = [manager logInWithUsername:self.usernameTextField.text password:self.passwordTextField.text]; [signingSignal subscribeError:^(NSError *error) { [self presentError:error]; } completed:^{ self.signing = YES; }]; }]; RAC(self, signing) = [[NSNotificationCenter.defaultCenter rac_addObserverForName:@"LOSignNotification" object:nil] mapReplace:@NO]; } @end
導入ReactiveCocoa框架ide
打開 終端(Terminal) 工具。進入到本身的工程目錄文件夾下:函數
haohao:TomRAC lewis$ vi Podfile
其中 TomRAC 是公司項目文件夾名稱工具
建立完成 Podfile 文件後,要在該文件內輸入以下內容:spa
platform :ios, '6.0' source 'https://github.com/CocoaPods/Specs.git' target 'TomRAC' do pod 'ReactiveCocoa', '~> 2.3.1' end
經過命令保存 Podfile 文件後,在 終端(Terminal) 輸入以下命令進行下載配置 ReactiveCocoa 框架:code
pod install
而後就會看到熟悉的下載提示:
Analyzing dependencies Downloading dependencies Installing ReactiveCocoa (2.3.1) Generating Pods project Integrating client project [!] From now on use `TomRAC.xcworkspace`.
打開工程後就可以使用ReactiveCocoa框架了。
現有個空工程,須要跳過配置步驟想先使用的同窗能夠先進行下載使用。