我已经将 RAC 与 objective-c 一起使用,但现在无法与 swift 一起使用。
我下载了 ReactiveCocoaBindings.swift 并将其包含在我的项目中。
<强> Here 临时宏。
我需要观察一个“链接”到 Collection View 的数组。
有了 objective-c,我可以用这段代码来完成:
[RACObserve(self, self.myArray) subscribeNext:^(NSArray* arr) {
[collectionView reloadData];
}];
在 ReactiveCocoa 桥接文件中有:
public func RACObserve(target: NSObject!, keyPath: String) -> RACSignal {
return target.rac_valuesForKeyPath(keyPath, observer: target)
}
那么..我该如何使用它?
最佳答案
RACObserve(self, "myArray").subscribeNext { [weak self] arr in
// cast arr to array if you want to use it inside block replace it with _ symbol otherwise
self?.collectionView.reloadData()
}
请记住,如果 myArray
属性在 swift 文件中定义,它应该有 dynamic
修饰符
关于ios - 快速使用 ReactiveCocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30307676/