为什么RAC更加适合编写Cocoa App?说这个之前,我们先来看下Web前端编程,因为有些相似之处。目前很火的AngularJS有一个很重要的特性:数据与视图绑定。就是当数据变化时,视图不需要额外的处理,便可正确地呈现最新的数据。而这也是RAC的亮点之一。RAC与Cocoa的编程模式,有点像AngularJS和jQuery。所以要了解RAC,需要先在观念上做调整。
-
1.RAC进行数据绑定KVO的例子:
// When self.username changes, logs the new name to the console. // // RACObserve(self, username) creates a new RACSignal that sends the current // value of self.username, then the new value whenever it changes. // -subscribeNext: will execute the block whenever the signal sends a value. [RACObserve(self, username) subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];
当每次self.username改变,都会自动执行subscribeNext,打出内容到控制台
-
2.RAC不仅仅是kvo,它还可以对信号进行过滤,筛选正确的信号,例如:
// Only logs names that starts with "j". // // -filter returns a new RACSignal that only sends a new value when its block // returns YES. [[RACObserve(self, username) filter:^(NSString *newName) { return [newName hasPrefix:@"j"]; }] subscribeNext:^(NSString *newName) { NSLog(@"%@", newName); }];
筛选前缀是j的username,打出到控制台
##Swift 与 Objective-C 中的使用区别:
设置请看 RACSwift
RAC
In Objective-C
:
RAC(self.collectionView,page) = RACObserve(self.collectionView,page);
In Swift
:
RAC(self.collectionView,"page") <= RACObserve(self.collectionView,"page")
RACObserve(self.collectionView,"page") => RAC(self.collectionView,"page")
RACObserve
In Objective-C
:
[[[RACObserve(self.collectionView, page)
map:^NSString*(NSNumber* newPage) {
return @"123";
}]
filter:^BOOL(NSString* newPage) {
return false;
}]
subscribeNext:^(NSString* text) {
NSLog(@"%@",text);
}];
In Swift
:
RACObserve(self.collectionView,"page")
.mapAs{
(newpage:NSNumber) -> NSString in
return "123"
}
.filterAs{
(newpage:NSString) -> Bool in
return false
}
.subscribeNextAs{
(text:String) -> () in
println(text)
}