我可以/应该如何用RC3替换我的KVO东西?

我可以/应该如何用RC3替换我的KVO东西?

问题描述:

我正在尝试将使用Facebook的KVOController的objc应用程序移植到Swift.鼓励我将 RC3 视为另一种更快捷的方法.我读了一些博客,并鼓励尝试一下.但是许多文档和博客似乎都以套接字和计时器为例.所以我现在有两个简单的问题:

I'm trying to port an objc app which uses Facebook's KVOController, to Swift. I've been encouraged to look at RC3 as an alternate and more Swiftish approach. I've read some blogs and I'm encouraged to give this a try. But much of the docs and blogs seem to concentrate on sockets and timers as examples. So I have two simple questions right now:

1)给出一个objc片段,例如:

1) Given an objc fragment like:

[self.KVOController observe: self.site keyPath: @"programs" options: NSKeyValueObservingOptionInitial block:^(id observer, id object, NSDictionary *change) {
    [self.tableView reloadData];
}];

用RC3 API重写此内容的简单方法是什么?当该视图卸载时,self.KVOController unobserve: self.site对应的RC3是什么?

What is the simple way to rewrite this with RC3 APIs? And what is the RC3 equivalent for self.KVOController unobserve: self.site that happens when that view unloads?

2)建议我使用迦太基获取RC3代码.我可以安全地将Cartfile与已经使用AlamofireSwiftyJSON的可可豆荚的项目混合吗?

2) It's recommended that I use Carthage to grab the RC3 code. Can I intermix a Cartfile safely with a project that is using cocoa pods already for Alamofire and SwiftyJSON?

免责声明:) 我还是整个ReactiveCocoa概念(尤其是RAC 3)的新手.我正在研究新的框架,因此我的解决方案可能不是API的预期用途.我很高兴收到他人的反馈并学习正确的方法.

Disclaimer :) I'm also new to the whole ReactiveCocoa concept and especially to RAC 3. I'm playing around with the new framework to learn, so my solution may not be the intended usage of the API. I'd be happy to receive feedback from others and learn the right way.

这是我对您的第一个问题的看法: 您可以将要观察的属性定义为MutableProperty.下面是带有String属性的最基本的示例.假设您有一个视图模型,它具有String属性,并且您想在视图控制器对象中进行观察.

Here's what I could come up with for your first question: You can define the property you'd want to observe as a MutableProperty. Below is the most basic example with a String property. Assume you have a view model, it has a String property and you want to observe this in your view controller object.

ViewModel.swift

class ViewModel {
    var testProp = MutableProperty<String>("")
}

ViewController.swift

class ViewController: UIViewController {
    private let viewModel = ViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()

        let testPropObserver = viewModel.testProp.producer
            testPropObserver.start(next: {value in
                println("new value \(value)")
            })

        viewModel.testProp.value = "you should see this in your log"
    }
}