重构ReactiveCocoa

时间:2013-06-14 19:53:52

标签: ios mvvm viewmodel reactive-cocoa

我在viewmodel中有以下代码:

@weakify(self);
[RACAbleWithStart(self.visitStartDate) subscribeNext:^(NSDate *visitStartDate) {
    @strongify(self);
    self.visit.startDate = visitStartDate;
}];
[RACAbleWithStart(self.visitEndDate) subscribeNext:^(NSDate *visitEndDate) {
    @strongify(self);
    self.visit.endDate = visitEndDate;
}];
[RACAbleWithStart(self.visitFocus) subscribeNext:^(NSString *focus) {
    @strongify(self);
    self.visit.actionPlan.focus = focus;
}];
[RACAbleWithStart(self.allDayVisit) subscribeNext: ^(NSNumber *allDayVisit) {
    @strongify(self);
    self.visit.allDay = allDayVisit;
}];

它基本上是属性绑定到私有属性。这完全是错误的解决方法,还是有更简洁的方式来编写上述代码?

1 个答案:

答案 0 :(得分:2)

试试这个:

RAC(self.visit, startDate) = RACAbleWithStart(self.visitStartDate);

来自RAC宏的标题注释:

// Lets you assign a keypath / property to a signal. The value of the keypath or
// property is then kept up-to-date with the latest value from the signal.
//
// If given just one argument, it's assumed to be a keypath or property on self.
// If given two, the first argument is the object to which the keypath is
// relative and the second is the keypath.
//
// Examples:
//
//  RAC(self.blah) = someSignal;
//  RAC(otherObject, blah) = someSignal;