0
0
iOS Swiftmobile~10 mins

@StateObject for observable objects 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 an observable object using @StateObject in a SwiftUI view.

iOS Swift
struct ContentView: View {
  @StateObject var viewModel = [1]()

  var body: some View {
    Text(viewModel.title)
  }
}
Drag options to blanks, or click blank then click option'
AObservableObject
BMyViewModel
CStateObject
DViewModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using @StateObject with a protocol name instead of a class.
Forgetting to create an instance by calling () after the class name.
2fill in blank
medium

Complete the code to update the view when the observable object changes.

iOS Swift
class MyViewModel: ObservableObject {
  @Published var count = 0
}

struct ContentView: View {
  @StateObject var viewModel = MyViewModel()

  var body: some View {
    Button(action: {
      viewModel.[1] += 1
    }) {
      Text("Count: \(viewModel.count)")
    }
  }
}
Drag options to blanks, or click blank then click option'
Acount
BobjectWillChange
Cupdate
Dstate
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to update objectWillChange directly.
Using a property that does not exist in the view model.
3fill in blank
hard

Fix the error in the code by completing the declaration of the observable object in the view.

iOS Swift
struct ContentView: View {
  [1] var viewModel = MyViewModel()

  var body: some View {
    Text("Count: \(viewModel.count)")
  }
}
Drag options to blanks, or click blank then click option'
A@StateObject
B@EnvironmentObject
C@ObservedObject
D@Published
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObservedObject which does not own the object and can cause multiple instances.
Using @Published which is only for properties inside observable objects.
4fill in blank
hard

Fill both blanks to declare an observable object class and use it with @StateObject in a SwiftUI view.

iOS Swift
class [1]: ObservableObject {
  @Published var message = "Hello"
}

struct ContentView: View {
  @StateObject var viewModel = [2]()

  var body: some View {
    Text(viewModel.message)
  }
}
Drag options to blanks, or click blank then click option'
AGreetingViewModel
BMessageViewModel
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the class and the instance.
Forgetting to add () when creating the instance.
5fill in blank
hard

Fill all three blanks to create an observable object with a published property and use it in a SwiftUI view with @StateObject.

iOS Swift
class [1]: ObservableObject {
  @Published var [2] = 0
}

struct ContentView: View {
  @StateObject var viewModel = [3]()

  var body: some View {
    Text("Value: \(viewModel.value)")
  }
}
Drag options to blanks, or click blank then click option'
ACounterModel
Bvalue
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using a property name different from the one displayed in the Text view.
Mismatching class name and instance name.