0
0
iOS Swiftmobile~10 mins

MVVM pattern in iOS Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a ViewModel class in Swift.

iOS Swift
class [1] {
  // ViewModel properties and methods
}
Drag options to blanks, or click blank then click option'
AViewModel
BModel
CViewController
DView
Attempts:
3 left
💡 Hint
Common Mistakes
Naming the ViewModel as ViewController or Model.
2fill in blank
medium

Complete the code to bind a ViewModel property to update the view using Combine.

iOS Swift
@Published var name: String = ""

var cancellables = Set<AnyCancellable>()

viewModel.$name
  .[1] { newName in
    self.label.text = newName
  }
  .store(in: &cancellables)
Drag options to blanks, or click blank then click option'
Asink
Bassign
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'assign' which requires a writable key path, or 'map' which transforms values.
3fill in blank
hard

Fix the error in the ViewModel initializer to properly initialize the model property.

iOS Swift
class ViewModel {
  private var model: Model

  init() {
    self.model = [1]
  }
}
Drag options to blanks, or click blank then click option'
Anil
BModel
Cself.model
DModel()
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning the type name or nil instead of an instance.
4fill in blank
hard

Fill both blanks to create a computed property in ViewModel that returns the model's title in uppercase.

iOS Swift
var uppercaseTitle: String {
  return model.title[1][2]()
}
Drag options to blanks, or click blank then click option'
A.
Buppercased
CuppercaseString
D()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'uppercaseString' which is Objective-C style, or missing the dot operator.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that filters model data with values greater than 10.

iOS Swift
let filteredData = model.data.filter { $0.value [1] 10 }.reduce(into: [:]) { $0[[2]] = [3] }
Drag options to blanks, or click blank then click option'
A>
B$1.key
C$1.value
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>', or mixing up key and value in reduce.