人们如何转换我的代码:
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。我读了关于反应式的文档,但我不明白如何在我的代码中实现反应式 API。 谁知道我需要怎么做,请告诉我。 还有一个知道最新版本ReactiveCocoa/ReactiveSwift的良好示例/示例/教程的人,请告诉我。
ReactiveCocoa 用于将动态数据(保存在视图模型中)绑定到 ViewController 的 UI。如果你的数据不是动态的(如果视图模型在视图控制器的生命周期内没有改变),你根本不需要使用reactivecocoa。但是,如果您的car
变量将发生变化,并且将使用单个视图控制器来显示多辆汽车,则反应可可将非常有用。您可以使用MutableProperty
类封装动态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将这些信号绑定到UI,以便每次更新viewModel.car.value
时UI都会自动更新新汽车数据!
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-但是,如果您的视图模型在视图控制器的整个生命周期中都在变化,ReactiveCocoa将允许您将可变数据绑定到UI,以便每当数据更改时,您的视图都会自动更新!