如何从ReactiveCocoa信号中获取“旧值”?

时间:2013-04-17 21:27:44

标签: ios objective-c key-value-observing reactive-programming reactive-cocoa

如果我像这样使用RACable:

[RACAbleWithStart(self.myProp) subscribeNext:^(id x) {
   // Do stuff

}];

如何才能访问myProp的旧值(在更改之前导致信号触发)?所以我可以像这样访问它:

[RACAbleWithStart(self.myProp) subscribeNext:^(id x) {
   // Do stuff
   id newValue = x;
   id oldValue = RAC_oldValue;
}];

1 个答案:

答案 0 :(得分:4)

我已成功使用此代码段:

[[object rac_valuesAndChangesForKeyPath:@"property" options:NSKeyValueObservingOptionOld observer:self] subscribeNext:^(RACTuple *tuple) {
    id newObject = tuple.first;
    NSDictionary *change = tuple.second;
    id oldObject = change[NSKeyValueChangeOldKey];
}];

来源:ReactiveCocoa documentation