ReactiveSwift - 更新 MutableProperty 的价值?

ReactiveSwift - Updating Value Of MutableProperty?

作为 question, which received 的后续,并使用以下示例...

class Model {
    let mapType = MutableProperty<MKMapType>(.standard)
}

class SomeViewController: UIViewController {

    let viewModel: Model
    var mapView: MKMapView!

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

    override func viewDidLoad() {
        super.viewDidLoad()
        mapView = // Create the map view
        viewModel.mapType.producer.startWithValues { [weak self] mapType in
            self?.mapView.mapType = mapType
        }
        // Rest of bindings
    }
    // The rest of the implementation...
}

class SomeOtherViewController: UIViewController {

    let viewModel: Model
    var segmentedControl: UISegmentedControl!

    init(viewModel: Model) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
        // Pretend we've setup the segmentedControl, and set its action to `updateMap(sender:)`...
        // The rest of the initialization...
    }

    func updateMap(sender: UISegmentedControl) {
        let newMapType =...
        self.viewModel.mapType = newMapType // How do I update this value?
    }
    // The rest of the implementation...
}

如何更新 Model.mapType 的值,并将其更改发送给任何观察者(如示例中的 SomeViewController)?

你只需要设置它的value 属性,这将触发更改自动发送给观察者。

self.viewModel.mapType.value = newMapType