一、Pod 安裝 RAC
- 在項目根目錄初始化
pod。
pod init
命令會自動創(chuàng)建一個 Podfile 文件。
- 在Podfile文件中添加依賴
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'uiview' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for uiview
pod 'ReactiveObjC', '~> 3.1.0'
pod 'Masonry', '~> 1.0.2'
end
- 安裝依賴
pod install
項目會自動轉(zhuǎn)換為 pod 管理模式,并安裝好所有清單中的依賴。
- 添加
RAC的頭文件引用
#import "ReactiveObjecC.h"
二、使用 RAC 連接組件
- 使用
@weakinfo(self)弱化引用, 在閉包中使用@strongify(self)強化引用。
- (void)bindComponents{
@weakify(self);
[[self.textView rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
@strongify(self);
self.myView.text = x;
}];
}