0
0
Swiftprogramming~20 mins

Composing property wrappers 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
Output of nested property wrappers

What is the output of the following Swift code using two composed property wrappers?

Swift
import Foundation

@propertyWrapper
struct Trimmed {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.trimmingCharacters(in: .whitespacesAndNewlines) }
    }
    init(wrappedValue: String) {
        self.wrappedValue = wrappedValue
    }
}

@propertyWrapper
struct Uppercased {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.uppercased() }
    }
    init(wrappedValue: String) {
        self.wrappedValue = wrappedValue
    }
}

struct User {
    @Trimmed @Uppercased var name: String
}

var user = User(name: "  swift  ")
print(user.name)
A"SWIFT"
B" SWIFT "
C"swift"
D"Swift"
Attempts:
2 left
💡 Hint

Remember the order in which property wrappers are applied matters.

Predict Output
intermediate
2:00remaining
Value of composed property wrappers with initial values

What will be the value of settings.title after this code runs?

Swift
@propertyWrapper
struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
    init(wrappedValue: String) {
        self.wrappedValue = wrappedValue
    }
}

@propertyWrapper
struct Suffix {
    private var value: String = ""
    private let suffix: String
    var wrappedValue: String {
        get { value }
        set { value = newValue + suffix }
    }
    init(wrappedValue: String, suffix: String) {
        self.suffix = suffix
        self.wrappedValue = wrappedValue
    }
}

struct Settings {
    @Suffix(suffix: "!") @Capitalized var title: String
}

var settings = Settings(title: "hello world")
print(settings.title)
A"Hello World!"
B"hello world!"
C"HELLO WORLD!"
D"Hello world!"
Attempts:
2 left
💡 Hint

Check the order of wrappers and how each modifies the string.

🔧 Debug
advanced
2:00remaining
Identify the error in composing property wrappers

What error does this Swift code produce when compiling?

Swift
@propertyWrapper
struct DoubleValue {
    var wrappedValue: Int {
        didSet { wrappedValue *= 2 }
    }
    init(wrappedValue: Int) {
        self.wrappedValue = wrappedValue
    }
}

@propertyWrapper
struct Increment {
    var wrappedValue: Int {
        get { value }
        set { value = newValue + 1 }
    }
    private var value: Int = 0
    init(wrappedValue: Int) {
        self.wrappedValue = wrappedValue
    }
}

struct Counter {
    @DoubleValue @Increment var count: Int
}

let c = Counter(count: 3)
AError: Missing initializer for property 'value' in Increment
BError: Recursive setter call causing infinite loop
CError: Cannot apply two property wrappers to the same property
DNo error, compiles successfully
Attempts:
2 left
💡 Hint

Look at the setter of DoubleValue and what happens when it sets wrappedValue.

📝 Syntax
advanced
2:00remaining
Which option correctly composes property wrappers with parameters?

Which of the following Swift code snippets correctly composes two property wrappers where one has a parameter?

A@Trimmed(suffix: "!") @Suffix var text: String = " hello "
B@Trimmed @Suffix(suffix: "!") var text: String = " hello "
C@Suffix @Trimmed(suffix: "!") var text: String = " hello "
D@Suffix(suffix: "!") @Trimmed var text: String = " hello "
Attempts:
2 left
💡 Hint

Remember how to apply parameters to property wrappers and their order.

🚀 Application
expert
3:00remaining
Number of times wrappedValue setter is called in composed wrappers

Given these two property wrappers and their composition, how many times is the wrappedValue setter called when setting example.value = 5?

Swift
@propertyWrapper
struct Logger {
    var wrappedValue: Int {
        didSet { print("Logger set to \(wrappedValue)") }
    }
    init(wrappedValue: Int) {
        self.wrappedValue = wrappedValue
    }
}

@propertyWrapper
struct Doubler {
    var wrappedValue: Int {
        get { value }
        set {
            value = newValue * 2
            print("Doubler set to \(value)")
        }
    }
    private var value: Int
    init(wrappedValue: Int) {
        self.value = wrappedValue * 2
        print("Doubler init with \(value)")
    }
}

struct Example {
    @Logger @Doubler var value: Int
}

var example = Example(value: 3)
example.value = 5
A1 time
B3 times
C2 times
D4 times
Attempts:
2 left
💡 Hint

Trace the setter calls from the outer wrapper to the inner wrapper.