ios - ReactiveCocoa 的 RACObserve 是不是要替换属性的 set 方法?

标签 ios reactive-cocoa

创建一个类的实例之后。我通过 RAC 宏绑定(bind)一些属性。

_theTable = [[ScrollableTable alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[_theTable setScrollEnabled:YES];
[_theTable setBounces:YES];
[_theTable setBackgroundColor:[UIColor whiteColor]];
[_theTable setShowsVerticalScrollIndicator:YES];
[_theTable setShowsHorizontalScrollIndicator:YES];
[self.view addSubview:_theTable];

RAC(self.theTable, dataVO) = RACObserve(self.tableVM, tableDataVO);
RAC(self.theTable, styleVO) = RACObserve(self.tableVM, tableStyleVO);

在 ScrollableTable 中,当 self.dataVO 和 self.styleVO 发生变化时,我尝试使用 RACObserve 宏来监听一些事件。当观察者第一次发出时,VO 是空的。所以我想知道使用 ReactiveCocoa 的正确方法是什么?

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code
    _isSeperateFill = YES;
    _isBorderStroke = NO;
    _isSeperatedStroke = YES;

    _contentWidth = 0;

    @weakify(self);
    [RACObserve(self, dataVO) subscribeNext:^(TableDataVO* dataVO){
        if( dataVO ){
            NSString* indexKey = [[dataVO.tableDataDictionary allKeys] objectAtIndex:0];
            @strongify(self);
            _keys = [self.dataVO.tableDataDictionary allKeys];
            _rows = [[self.dataVO.tableDataDictionary objectForKey:indexKey] count];

        }
    }];


    [RACObserve(self, styleVO) subscribeNext:^(TableStyleVO* styleVO){
        if( styleVO ){
            @strongify(self);
            self.styleVO.tableHeaderLineHorizontalMargin = styleVO.tableWidth / [_keys count] / 2;
        }
    }];

}
return self;
}

最佳答案

是的,这是正确的。当他们第一次被观察到时,他们不会有他们的值(value)设定。然后,您应该得到一个在信号上发出的具有初始值的子序列事件。

如果你不想要第一个值,就跳过它!

RACSignal *skipped = [RACObserve(self.tableVM, tableDataVO) skip:1];

关于ios - ReactiveCocoa 的 RACObserve 是不是要替换属性的 set 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320040/

相关文章:

ios - 将数据从 iOS 查询传递到 Watch

ios - 为什么从 flattenMap 返回 empty 不会触发 subscribeCompleted

objective-c - 使用 ReactiveCocoa 触发模型持久化有意义吗

objective-c - ReactiveCocoa 5.0 中的 RACObserve(object, keyPath)

ios - 如何在不使用 [RACSignal createSignal] 的情况下返回 RACSignal

swift - 信号 : Collect values over time interval

ios - LLVM 错误 : Call parameter type does not match function signature

ios - 如何在 Swift 中为 Collection View 添加边框?

ios - 在iOS的保管箱中未获取文件大小

ios - iOS 上的低级共享内存