我只是在尝试使用 WeatherViewController 创建一个天气应用程序,并使用单元格来显示 tableView ,当单元格被轻击时会导致 WeatherDetailsViewController 。
我将装箱方式用于绑定(bind),如果在下面的示例中同时在模型和 viewModel 中都设置动态类型,则感到困惑。你会明白我的意思的。
这是拳击课
class Dynamic<T>: Decodable where T: Decodable {
typealias Listener = (T) -> ()
var listener: Listener?
var value: T {
didSet {
listener?(value)
}
}
func bind(listener: @escaping Listener) {
self.listener = listener
self.listener?(self.value)
}
init(_ value: T) {
self.value = value
}
private enum CodingKeys: CodingKey {
case value
}
}
这是天气模型结构
struct Weather: Decodable {
let date: Dynamic<Int>
let description: Dynamic<String>
let maxTemperature: Dynamic<Double>
private enum CodingKeys: String, CodingKey {
case date = "time"
case description = "summary"
case maxTemperature = "temperatureMax"
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
date = try Dynamic(container.decode(Int.self, forKey: .date))
description = try Dynamic(container.decode(String.self, forKey: .description))
maxTemperature = try Dynamic(container.decode(Double.self, forKey: .maxTemperature))
}
}
这是我的WeatherListViewModel和WeatherViewModel
在我的WeatherViewModel内部,我已将类型指定为Dynamic,但在模型中也将其分配为要绑定(bind)在WeatherDetailsViewController中,对吗?
class WeatherListViewModel {
var weatherViewModels: [WeatherViewModel]
private var sessionProvider: URLSessionProvider
init(sessionProvider: URLSessionProvider) {
self.sessionProvider = sessionProvider
self.weatherViewModels = [WeatherViewModel]()
}
func numberOfRows(inSection section: Int) -> Int {
return weatherViewModels.count
}
func modelAt(_ index: Int) -> WeatherViewModel {
return weatherViewModels[index]
}
func didSelect(at indexPath: Int) -> WeatherViewModel {
return weatherViewModels[indexPath]
}
}
这是用于网络获取的WeatherListViewModel扩展,在其中我初始化WeatherViewModel
func fetchWeatherLocation(withLatitude latitude: CLLocationDegrees, longitude: CLLocationDegrees, completion: @escaping handler) {
sessionProvider.request(type: WeatherWrapper.self, service: WeatherService.specificLocation, latitude: latitude, longitude: longitude) { [weak self] result in
switch result {
case let .success(weatherWrapper):
let weathers = weatherWrapper.daily.weathers
self?.weatherViewModels = weathers.map {
return WeatherViewModel(weather: $0)
}
completion()
case let .failure(error):
print("Error: \(error)")
}
}
}
这是WeatherViewModel
struct WeatherViewModel {
private(set) var weather: Weather
var temperature: Dynamic<Double>
var date: Dynamic<Int>
var description: Dynamic<String>
init(weather: Weather) {
self.weather = weather
self.temperature = Dynamic(weather.maxTemperature)
self.date = Dynamic(weather.date)
self.description = Dynamic(weather.description)
}
}
这是我的WeatherDetailsViewController
在这里,我将绑定(bind)分别分配给标签以获取更改
class WeatherDetailsViewController: UIViewController {
@IBOutlet private var imageView: UIImageView!
@IBOutlet private var cityLabel: UILabel!
@IBOutlet private var dateLabel: UILabel!
@IBOutlet private var descriptionLabel: UILabel!
@IBOutlet private var temperatureLabel: UILabel!
var viewModel: WeatherViewModel?
override func viewDidLoad() {
super.viewDidLoad()
setupVMBinding()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationItem.largeTitleDisplayMode = .never
}
private func setupVMBinding() {
if let viewModel = viewModel {
viewModel.date.bind {
self.dateLabel.text = $0.toString()
}
viewModel.temperature.bind {
self.temperatureLabel.text = "\($0)"
}
viewModel.description.bind {
self.descriptionLabel.text = $0.description
}
}
}
}
问题是,我是否只是在模型和viewModel中重复编写了动态类型?有没有更好的方法可以做到这一点,或者我在正确的轨道上吗?很抱歉,长代码示例。
最佳答案
我认为您在Weather
模型中重复编写了Dynamic。
它不需要是动态类型。
您可以创建一个GenericDataSource
class GenericDataSource<T>: NSObject {
var data: Dynamic<T>?
}
在您的 View 模型中。这将引用您的天气模型,而无需创建动态类型。
class WeatherViewModel {
var dataSource: GenericDataSource<Weather>?
....
}
在View Controller内部
class WeatherDetailsViewController {
var viewModel: WeatherViewModel?
override func viewDidLoad() {
viewModel = ViewModel()
var dataSource = GenericDataSource<Weather>()
dataSource.data = Dynamic(Weather)
viewModel.dataSource = dataSource
setupVMBinding()
}
private func setupVMBinding() {
viewModel?.dataSource?.data?.bind {
self.dateLabel.text = $0.date
self.temperatureLabel.text = "\($0.maxTemperature)"
self.descriptionLabel.text = $0.description
}
}
}
关于swift - Swift MVVM与拳击绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59148698/