У меня есть функция, которая возвращает данные о погоде (не в массиве). Эта функция инициализируется в ViewModel. И в качестве завершения я использую typealias. Но когда данные в viewModel получены, и я передаю их в View, ничего не происходит в fuction ab (). Это код ViewModel:
protocol Alias {
typealias type = ([Convertible], Convertible) -> Void
typealias DidFetchUserLocationCompletion = (Location) -> Void
}
final class MainControllerViewModel: NSObject, Alias {
var didFetchUserLocation: DidFetchUserLocationCompletion?
var completion: type?
private var weather = [Convertible]()
private var weatherModel = [DailyForecast]()
private var initWeather: WeatherOneDayInitializer!
private let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
return dateFormatter
}()
func getLocation() {
LocationManagerSaver.shared.fetchLocation { [weak self] (location, error) in
guard let location = location else { return }
self?.didFetchUserLocation?(location)
}
}
}
, и это код вида:
final class ViewController: UIViewController {
let locationManager = CLLocationManager()
fileprivate var weather = [Convertible]()
fileprivate var mainViewModel = MainControllerViewModel()
fileprivate var viewModel: MainControllerViewModel? {
didSet {
guard let viewModel = viewModel else { return }
ab(viewM: viewModel)
}
}
override func viewDidLoad() {
viewModel = MainControllerViewModel()
locationManager.delegate = self
super.viewDidLoad()
let nib = UINib(nibName: CollectionViewCell.reuseIdentifier, bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: CollectionViewCell.reuseIdentifier)
}
func ab(viewM: MainControllerViewModel) {
viewM.completion = { item, one in
DispatchQueue.main.async {
print(one.temperature)
}
}
}
}