2017-04-10 58 views
1

的人我怎麼可以把我的代碼:簡單的代碼迅疾reactiveswift

struct CarModel { 
    var model: String? 
    var make: String? 
    var kilowatts: Int? 
    var photoURL: String? 

    init(model: String, make: String, kilowatts: Int, photoURL: String) { 
     self.model = model 
     self.make = make 
     self.kilowatts = kilowatts 
     self.photoURL = photoURL 
    } 
} 

和:

class CarViewModel { 
    private var car: Car? 

    static let HPperKW = 1.34102209 

    var modelText: String? { 
     return car?.model 
    } 
    var makeText: String? { 
     return car?.make 
    } 
    var horsepowerText: String? { 
     guard let kilowatts = car?.kilowatts else { return nil } 
     let HP = Int(round(Double(kilowatts) * CarViewModel.HPperKW)) 
     return "\(HP) HP" 
    } 
    var titleText: String? { 
     guard let make = car?.make, let model = car?.model else { return nil } 
     return "\(make) \(model)" 
    } 
    var photoURL: URL? { 
     guard let photoURL = car?.photoURL else { return nil } 
     return URL(string: photoURL) 
    } 

    init(_ car: Car) { 
     self.car = car 
    } 
} 

到ReactiveCocoa/ReactiveSwift。我讀了。有關Reactive的文檔,但我不明白我如何在我的代碼中實現Reactive API。 誰知道我需要怎麼做,請告訴我。 另外一個誰知道最後的版本ReactiveCocoa/ReactiveSwift的好樣本/例子/教程,請告訴我。

回答

1

ReactiveCocoa是綁定動態數據(保存在您的viewmodel中)到ViewController的UI。如果你的數據不是動態的(如果viewmodel在viewcontroller的生命週期中沒有改變),你根本不需要使用reactivecocoa。但是,如果您的car變量將更改並且將使用單個視圖控制器來顯示多輛汽車,那麼reactivecocoa將非常有用。您可以使用MutableProperty類來封裝動態car變量並創建信號,以便每當car屬性更改時更新ViewController。

class CarViewModel { 
    let car: MutableProperty<Car> 

    init(_ car: Car) { 
    self.car = MutableProperty(car) 
    } 

    var modelTextSignal: SignalProducer<String, NoError> { 
    return car.producer.map { $0.model } 
    } 

    var makeTextSignal: SignalProducer<String, NoError> { 
    return car.producer.map { $0.make } 
    } 

    var horsepowerTextSignal: SignalProducer<String, NoError> { 
    return car.producer.map { car in 
     let HP = Int(round(Double(car.kilowatts) * CarViewModel.HPperKW)) 
     return "\(HP) HP" 
    } 
    } 

    var titleTextSignal: SignalProducer<String, NoError> { 
    return car.producer.map { "\($0.make) \($0.model)" } 
    } 

    var photoURLSignal: SignalProducer<URL?, NoError> { 
    return car.producer.map { URL(string: $0.photoURL) } 
    } 
} 

現在,我們有一大堆的代表了隨時間變化的car數據信號,並可以使用ReactiveCocoa這些信號綁定到用戶界面,使用戶界面自動與新車數據每次viewModel.car.value是更新更新!

class CarViewController: UIViewController { 
    @IBOutlet modelLabel: UILabel! 
    @IBOutlet makeLabel: UILabel! 
    @IBOutlet horsepowerLabel: UILabel! 
    @IBOutlet titleLabel: UILabel! 
    @IBOutlet image: UIImageView! 

    var viewModel: CarViewModel! 

    override func viewDidLoad() { 
    self.modelLabel.reactive.text <~ self.viewModel.modelTextSignal 
    self.makeLabel.reactive.text <~ self.viewModel.makeTextSignal 
    self.horsepowerLabel.reactive.text <~ self.viewModel.horsepowerTextSignal 
    self.titleLabel.reactive.text <~ self.viewModel.titleTextSignal 
    self.viewModel.photoURLSignal.startWithValues { [weak self] url in 
     self?.setImageFromUrl(url) 
    } 
    } 

    func displayNewCar() { 
    self.viewModel.car.value = aRandomCar() 
    } 

    private func setImageFromUrl(url: URL?) { 
    //download url and display in UIImageView 
    } 
    private func aRandomCar() -> Car { 
    //return a Car object 
    } 
} 

所以你可以看到,如果你只需要在您的視圖控制器來顯示一個不變的車對象,ReactiveCocoa是unneeded--然而,如果你的視圖模型在整個的ViewController的生命週期的過程中不斷變化,reactivecocoa會允許您將可變數據綁定到UI,以便在數據更改時自動更新視圖!