People how I can convert my code:
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
}
}
and:
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
}
}
to ReactiveCocoa/ReactiveSwift. I read off. documentation about Reactive but I did not understand how I can implemented Reactive API to my code. Who knows how I need to do it, please tell me. And else one who knows good samples/examples/tutorials for last version ReactiveCocoa/ReactiveSwift, please tell me.
ReactiveCocoa is for binding dynamic data (held in your viewmodel) to the UI of a ViewController. If your data is not dynamic (if the viewmodel does not change over the course of the viewcontroller's lifetime) you do not need to use reactivecocoa at all. However, if your
carvariable will change and a single viewcontroller will be used to display multiple cars, reactivecocoa will be very useful. You can use theMutablePropertyclass to encapsulate the dynamiccarvariable and create signals that will update the ViewController whenever the car property changes.now, we have a bunch of signals that represent the changing
cardata over time, and can use ReactiveCocoa to bind these signals to the UI so that the UI automatically updates with the new car data every timeviewModel.car.valueis updated!So you can see how if you only need to display a single unchanging car object in your view controller, ReactiveCocoa is unneeded-- however, if your viewmodel is changing throughout the course of the viewcontroller's lifetime, reactivecocoa will allow you to bind your mutable data to the UI so that your views automatically update whenever the data changes!