0
0
iOS Swiftmobile~10 mins

ObservableObject protocol 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 make the class notify views about changes.

iOS Swift
class UserSettings: [1] {
  @Published var score = 0
}
Drag options to blanks, or click blank then click option'
AObservableObject
BView
CState
DApp
Attempts:
3 left
💡 Hint
Common Mistakes
Using View or State instead of ObservableObject.
Forgetting to conform to any protocol.
2fill in blank
medium

Complete the code to mark a property that triggers view updates.

iOS Swift
class Settings: ObservableObject {
  [1] var isDarkMode = false
}
Drag options to blanks, or click blank then click option'
A@State
B@EnvironmentObject
C@ObservedObject
D@Published
Attempts:
3 left
💡 Hint
Common Mistakes
Using @State inside ObservableObject class.
Using @ObservedObject or @EnvironmentObject incorrectly.
3fill in blank
hard

Fix the error in the view to observe the object correctly.

iOS Swift
struct ContentView: View {
  [1] var settings = Settings()

  var body: some View {
    Text(settings.isDarkMode ? "Dark" : "Light")
  }
}
Drag options to blanks, or click blank then click option'
A@EnvironmentObject
B@Published
C@StateObject
D@ObservedObject
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObservedObject when the view creates the object.
Using @Published in a view property.
4fill in blank
hard

Fill both blanks to observe an existing object passed from parent view.

iOS Swift
struct DetailView: View {
  [1] var settings: Settings

  var body: some View {
    Toggle("Dark Mode", isOn: $settings.[2])
  }
}
Drag options to blanks, or click blank then click option'
A@ObservedObject
BisDarkMode
C@StateObject
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using @StateObject for passed-in objects.
Binding to a property not marked @Published.
5fill in blank
hard

Fill all three blanks to create and observe an object in a parent view and pass it to a child view.

iOS Swift
struct ParentView: View {
  [1] var settings = Settings()

  var body: some View {
    ChildView([2]: settings)
  }
}

struct ChildView: View {
  [3] var settings: Settings

  var body: some View {
    Text(settings.isDarkMode ? "Dark" : "Light")
  }
}
Drag options to blanks, or click blank then click option'
A@StateObject
Bsettings
C@ObservedObject
D@Published
Attempts:
3 left
💡 Hint
Common Mistakes
Using @ObservedObject in parent view for object creation.
Not passing the object with the correct parameter name.
Using @Published in views.