Swift3 简单的例子

Swift3 简单的例子,swift3,reactive-cocoa,Swift3,Reactive Cocoa,我读过他们的书,浏览了他们精彩的游乐场示例,搜索了S.O.,并达到了我的极限,但我一辈子都不知道如何使用ReactiveSwift 鉴于以下情况 class SomeModel { var mapType: MKMapType = .standard var selectedAnnotation: MKAnnotation? var annotations = [MKAnnotation]() var enableRouteButton = false

我读过他们的书,浏览了他们精彩的游乐场示例,搜索了S.O.,并达到了我的极限,但我一辈子都不知道如何使用ReactiveSwift

鉴于以下情况

class SomeModel {
    var mapType: MKMapType = .standard
    var selectedAnnotation: MKAnnotation?
    var annotations = [MKAnnotation]()
    var enableRouteButton = false

    // The rest of the implementation...
}

class SomeViewController: UIViewController {

    let model: SomeModel
    let mapView = MKMapView(frame: .zero) // It's position is set elsewhere
    @IBOutlet var routeButton: UIBarButtonItem?

    init(model: SomeModel) {
        self.model = model
        super.init(nibName: nil, bundle: nil)
    }


    // The rest of the implementation...
}
..如何使用ReactiveSwift使用
SomeModel
中的值初始化
SomeViewController
,然后在
SomeModel
中的值更改时更新
SomeViewController

我以前从未使用过反应式,但我读到的每一篇文章都让我相信这应该是可能的。这让我发疯


我意识到,与我在本例中试图实现的目标相比,ResponsiveSwift还有很多,但如果有人能用它来帮助我开始,我将不胜感激。我希望一旦我得到了这个部分,其余的将只是“单击”。

首先,您将希望在模型中使用
MutableProperty
而不是普通类型。这样,您就可以观察它们的变化

class Model {
    let mapType = MutableProperty<MKMapType>(.standard)
    let selectedAnnotation = MutableProperty<MKAnnotation?>(nil)
    let annotations = MutableProperty<[MKAnnotation]>([])
    let enableRouteButton = MutableProperty<Bool>(false)
}
类模型{
设mapType=MutableProperty(.standard)
让selectedAnnotation=MutableProperty(nil)
let annotations=MutableProperty([])
let enableRouteButton=MutableProperty(false)
}
然后,在ViewController中,您可以绑定这些内容并观察这些内容(无论多么必要):

class SomeViewController: UIViewController {

    let viewModel: Model
    let mapView = MKMapView(frame: .zero) // It's position is set elsewhere
    @IBOutlet var routeButton: UIBarButtonItem!

    init(viewModel: Model) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        routeButton.reactive.isEnabled <~ viewModel.enableRouteButton
        viewModel.mapType.producer.startWithValues { [weak self] mapType in
            // Process new map type
        }
        // Rest of bindings
    }
    // The rest of the implementation...
}
类SomeViewController:UIViewController{
让视图模型:模型
让mapView=MKMapView(帧:.0)//它的位置在别处设置
@IBVAR路由按钮:UIBarButtonItem!
初始化(视图模型:模型){
self.viewModel=viewModel
super.init(nibName:nil,bundle:nil)
}
重写func viewDidLoad(){
super.viewDidLoad()

routeButton.reactive.I我发现在我阅读了你的答案后,你刚才听到的“点击”是有意义的。感谢你在示例中对其进行分解,因为它起到了很大的作用!很高兴我能提供帮助