0
0
Swiftprogramming~20 mins

Built-in property wrappers (@State, @Published) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Property Wrapper Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this SwiftUI @State example?

Consider this SwiftUI view code snippet. What will be printed to the console when the button is tapped once?

Swift
import SwiftUI

struct ContentView: View {
    @State private var count = 0

    var body: some View {
        Button("Tap me") {
            count += 1
            print("Count is now \(count)")
        }
    }
}
ACount is now 0
BNo output printed
CCount is now 1
DCount is now 2
Attempts:
2 left
💡 Hint

Remember @State stores a value that updates when changed.

Predict Output
intermediate
2:00remaining
What happens when @Published property changes in this ObservableObject?

Given this Swift class, what will be printed when model.name = "Alice" is executed?

Swift
import Foundation
import Combine

class UserModel: ObservableObject {
    @Published var name: String = ""
}

let model = UserModel()
let cancellable = model.$name.sink { newName in
    print("Name changed to: \(newName)")
}

model.name = "Alice"
AName changed to: Alice
BNo output
CName changed to:
DRuntime error
Attempts:
2 left
💡 Hint

@Published triggers updates to subscribers when the property changes.

🔧 Debug
advanced
2:30remaining
Why does this SwiftUI view not update when @Published changes?

Look at this code. The view does not update when model.name changes. What is the cause?

Swift
import SwiftUI

class UserModel: ObservableObject {
    @Published var name = ""
}

struct ContentView: View {
    @ObservedObject var model = UserModel()

    var body: some View {
        Text(model.name)
    }
}

let model = UserModel()
model.name = "Bob"
AThe Text view cannot display @Published properties
BThe @Published property must be private
CThe model instance is not initialized
DThe model property in ContentView should be marked with @ObservedObject
Attempts:
2 left
💡 Hint

SwiftUI views must observe ObservableObject with @ObservedObject to update.

📝 Syntax
advanced
1:30remaining
Which option correctly declares a @State property in SwiftUI?

Choose the correct syntax to declare a @State property named isOn initialized to false.

A@State private var isOn = false
Bvar @State isOn = false
C@State var isOn: Bool = false
D@State let isOn = false
Attempts:
2 left
💡 Hint

@State properties are usually private vars.

🚀 Application
expert
3:00remaining
How to combine @Published and @State for shared data in SwiftUI?

You want a SwiftUI view to update when a shared model's property changes. The model uses @Published. Which approach correctly connects the model to the view's @State?

AUse @Published in the view instead of @State
BUse @ObservedObject for the model in the view and bind the property directly, not @State
CUse @StateObject for the model and ignore @Published properties
DDeclare the model as @State and access the @Published property inside
Attempts:
2 left
💡 Hint

Think about which property wrapper watches ObservableObject changes.