0
0
Swiftprogramming~20 mins

Why property wrappers reduce boilerplate in Swift - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Property Wrapper Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple property wrapper usage
What is the output of this Swift code using a property wrapper?
Swift
import Foundation

@propertyWrapper
struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
}

struct Person {
    @Capitalized var name: String
}

var p = Person()
p.name = "john doe"
print(p.name)
A"john doe"
B"John Doe"
C"JOHN DOE"
D"john Doe"
Attempts:
2 left
💡 Hint
Think about what the property wrapper does when setting the value.
🧠 Conceptual
intermediate
1:30remaining
Why do property wrappers reduce boilerplate?
Which of the following best explains why property wrappers reduce boilerplate code in Swift?
AThey replace the need for classes and structs entirely.
BThey automatically generate UI elements for properties without extra code.
CThey allow you to write custom getter and setter logic once and reuse it across multiple properties.
DThey make all properties immutable by default.
Attempts:
2 left
💡 Hint
Think about how property wrappers help avoid repeating code.
🔧 Debug
advanced
2:30remaining
Identify the error in this property wrapper usage
What error will this Swift code produce?
Swift
@propertyWrapper
struct NonEmpty {
    var wrappedValue: String {
        didSet {
            if wrappedValue.isEmpty {
                wrappedValue = "Default"
            }
        }
    }
    init(wrappedValue: String) {
        self.wrappedValue = wrappedValue
        if wrappedValue.isEmpty {
            self.wrappedValue = "Default"
        }
    }
}

struct User {
    @NonEmpty var username: String
}

let u = User(username: "")
ARuntime error: infinite recursion in didSet
BCompile-time error: missing initial value for wrappedValue
CNo error, username is set to "Default"
DRuntime error: nil value assigned to non-optional
Attempts:
2 left
💡 Hint
Check what happens when wrappedValue is set inside didSet.
Predict Output
advanced
2:30remaining
Output of property wrapper with projected value
What will be printed by this Swift code?
Swift
@propertyWrapper
struct Logged<Value> {
    private var value: Value
    var wrappedValue: Value {
        get { value }
        set {
            print("Setting value to \(newValue)")
            value = newValue
        }
    }
    var projectedValue: String {
        "Value is currently \(value)"
    }
    init(wrappedValue: Value) {
        self.value = wrappedValue
    }
}

struct Config {
    @Logged var setting: Int = 10
}

var c = Config()
print(c.$setting)
c.setting = 20
A
Value is currently 10
Setting value to 20
B
Setting value to 10
Value is currently 10
Setting value to 20
C
Setting value to 10
Setting value to 20
Value is currently 20
D
Value is currently 20
Setting value to 20
Attempts:
2 left
💡 Hint
Remember when the setter print runs and what $property means.
🧠 Conceptual
expert
2:00remaining
How do property wrappers improve code maintainability?
Which statement best describes how property wrappers improve maintainability in Swift code?
AThey replace the need for unit tests by ensuring property correctness.
BThey automatically document properties with comments, reducing the need for manual documentation.
CThey enforce strict type checking at compile time, preventing runtime errors.
DThey centralize common property logic, making it easier to update behavior in one place rather than many.
Attempts:
2 left
💡 Hint
Think about how changing one property wrapper affects all properties using it.