Bird
Raised Fist0
iOS Swiftmobile~20 mins

@Published properties in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Published Property Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What happens when a @Published property changes?
Consider a SwiftUI view observing an ObservableObject with a @Published property. What is the immediate effect on the UI when this property changes?
ANothing happens until the view is manually refreshed.
BThe property change is ignored unless explicitly notified.
CThe app crashes due to state inconsistency.
DThe UI automatically updates to reflect the new property value.
Attempts:
2 left
💡 Hint
Think about how SwiftUI reacts to changes in ObservableObject properties marked with @Published.
📝 Syntax
intermediate
2:00remaining
Identify the correct @Published property declaration
Which of the following is the correct way to declare a @Published property inside an ObservableObject class in Swift?
Aclass Model: ObservableObject { @Publish var count: Int = 0 }
Bclass Model: ObservableObject { published var count: Int = 0 }
Cclass Model: ObservableObject { @Published var count: Int = 0 }
Dclass Model: ObservableObject { var count: Int = 0 @Published }
Attempts:
2 left
💡 Hint
Remember the exact spelling and placement of the @Published attribute.
❓ lifecycle
advanced
2:00remaining
When does a SwiftUI view update with @Published?
Given an ObservableObject with multiple @Published properties, when does a SwiftUI view observing it update?
AThe view updates whenever any @Published property changes, regardless of usage.
BThe view updates only when the specific @Published property it uses changes.
CThe view updates only when the ObservableObject is recreated.
DThe view never updates automatically; manual refresh is required.
Attempts:
2 left
💡 Hint
Think about how SwiftUI tracks ObservableObject changes.
🔧 Debug
advanced
2:00remaining
Why does the UI not update after changing a @Published property?
Consider this code snippet: class Counter: ObservableObject { @Published var value: Int = 0 } struct ContentView: View { @StateObject var counter = Counter() var body: some View { Text("Count: \(counter.value)") } } If the value property changes but the UI does not update, what is the most likely cause?
AThe Counter instance was created outside the view and not marked with @StateObject.
BThe @Published property was declared as private.
CThe Text view does not support dynamic updates.
DThe ObservableObject protocol was not conformed to.
Attempts:
2 left
💡 Hint
Check how the ObservableObject instance is created and stored in the view.
🧠 Conceptual
expert
3:00remaining
What is the underlying mechanism of @Published in Swift?
Which statement best describes how the @Published property wrapper works internally in Swift's Combine framework?
A@Published stores the property value in UserDefaults automatically.
B@Published creates a publisher that emits events before the property changes, notifying subscribers.
C@Published delays property changes until the view requests an update.
D@Published converts the property into a computed property without storage.
Attempts:
2 left
💡 Hint
Think about Combine's role in reactive programming and how @Published connects to it.

Practice

(1/5)
1. What is the main purpose of marking a property with @Published in SwiftUI?
easy
A. To make the property constant
B. To make the property private
C. To automatically notify views when the property changes
D. To prevent the property from being saved

Solution

  1. Step 1: Understand @Published role

    @Published marks a property to send updates when its value changes.
  2. Step 2: Connect to UI updates

    This automatic notification helps SwiftUI views update when data changes.
  3. Final Answer:

    To automatically notify views when the property changes -> Option C
  4. Quick Check:

    @Published = automatic update notification [OK]
Hint: Remember: @Published means auto-update UI on change [OK]
Common Mistakes:
  • Thinking @Published makes property private
  • Confusing @Published with constant declaration
  • Assuming @Published saves data permanently
2. Which of the following is the correct way to declare a @Published property inside an ObservableObject class?
easy
A. struct Model: ObservableObject { @Published var count: Int = 0 }
B. class Model: ObservableObject { @Published var count: Int = 0 }
C. class Model { @Published var count: Int = 0 }
D. class Model: ObservableObject { var count: @Published Int = 0 }

Solution

  1. Step 1: Check class and ObservableObject conformance

    @Published must be inside a class conforming to ObservableObject, so class Model: ObservableObject is correct.
  2. Step 2: Verify property declaration syntax

    The correct syntax is @Published var count: Int = 0, not var count: @Published Int.
  3. Final Answer:

    class Model: ObservableObject { @Published var count: Int = 0 } -> Option B
  4. Quick Check:

    Correct syntax = class Model: ObservableObject { @Published var count: Int = 0 } [OK]
Hint: Remember: @Published goes before var inside ObservableObject class [OK]
Common Mistakes:
  • Using struct instead of class for ObservableObject
  • Placing @Published after the type
  • Not conforming class to ObservableObject
3. Given the code below, what will be printed when model.count = 5 is executed?
class Model: ObservableObject {
  @Published var count: Int = 0
}

let model = Model()
model.$count.sink { print("Count changed to \($0)") }
medium
A. Count changed to 5
B. Count changed to 0
C. No output
D. Compile error

Solution

  1. Step 1: Understand @Published publisher

    @Published creates a publisher accessible via $property, here $count, which emits new values on change.
  2. Step 2: Analyze sink subscription and assignment

    The sink closure prints the new value when count changes. Setting model.count = 5 triggers the print with 5.
  3. Final Answer:

    Count changed to 5 -> Option A
  4. Quick Check:

    @Published publishes new values = Count changed to 5 [OK]
Hint: Remember: $property publishes changes, sink prints new value [OK]
Common Mistakes:
  • Expecting output before assignment
  • Confusing $count with count
  • Thinking no output without explicit call
4. Identify the error in this code snippet:
class Model: ObservableObject {
  @Published var name: String

  init() {
    name = ""
  }
}

let model = Model()
model.name = "Alice"
medium
A. Missing initial value for @Published property
B. ObservableObject conformance is missing
C. Cannot assign to @Published property outside class
D. No error, code works fine

Solution

  1. Step 1: Check @Published property initialization

    The property name is initialized in init(), which is valid for non-optional properties.
  2. Step 2: Verify ObservableObject conformance and usage

    Class conforms to ObservableObject and property is assigned outside class, which is allowed.
  3. Final Answer:

    No error, code works fine -> Option A
  4. Quick Check:

    Proper init and conformance = No error, code works fine [OK]
Hint: Remember: @Published properties can be initialized in init() [OK]
Common Mistakes:
  • Assuming @Published needs default value inline
  • Thinking ObservableObject conformance is missing
  • Believing external assignment is disallowed
5. You want to create a SwiftUI app where a @Published property updates a label text automatically. Which of the following is the best way to connect the @Published property to the UI?
hard
A. Use @EnvironmentObject without passing the object
B. Use @State in the view for the property
C. Manually call setNeedsDisplay() after changing the property
D. Use @ObservedObject in the view to watch the ObservableObject

Solution

  1. Step 1: Understand property wrappers for UI updates

    @Published properties notify changes, but the view must observe the object using @ObservedObject to update UI automatically.
  2. Step 2: Evaluate other options

    @State is for local view state, not external objects; setNeedsDisplay() is UIKit, not SwiftUI; @EnvironmentObject requires injection.
  3. Final Answer:

    Use @ObservedObject in the view to watch the ObservableObject -> Option D
  4. Quick Check:

    @Published + @ObservedObject = auto UI update [OK]
Hint: Connect @Published with @ObservedObject in views [OK]
Common Mistakes:
  • Using @State instead of @ObservedObject for external data
  • Trying manual UI refresh calls in SwiftUI
  • Using @EnvironmentObject without proper injection
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard