Complete the code to declare a ViewModel class in Swift.
class [1] { // ViewModel properties and methods }
The ViewModel class holds the data and logic for the view. Naming it ViewModel follows the MVVM pattern.
Complete the code to bind a ViewModel property to update the view using Combine.
@Published var name: String = "" var cancellables = Set<AnyCancellable>() viewModel.$name .[1] { newName in self.label.text = newName } .store(in: &cancellables)
The sink operator subscribes to changes and executes the closure to update the UI.
Fix the error in the ViewModel initializer to properly initialize the model property.
class ViewModel { private var model: Model init() { self.model = [1] } }
You must create a new instance of Model by calling Model() to initialize the property.
Fill both blanks to create a computed property in ViewModel that returns the model's title in uppercase.
var uppercaseTitle: String {
return model.title[1][2]()
}Use the dot operator to call the uppercased() method on the string.
Fill all three blanks to create a dictionary comprehension that filters model data with values greater than 10.
let filteredData = model.data.filter { $0.value [1] 10 }.reduce(into: [:]) { $0[[2]] = [3] }The filter uses > to keep values greater than 10. The reduce builds a dictionary using the key and value from each element.