0
0
Swiftprogramming~20 mins

Property wrappers with configuration 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 a property wrapper with initial configuration
What is the output of this Swift code using a property wrapper with configuration?
Swift
import Foundation

@propertyWrapper
struct Clamped {
    private var value: Int
    private let range: ClosedRange<Int>

    init(wrappedValue: Int, _ range: ClosedRange<Int>) {
        self.range = range
        self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
    }

    var wrappedValue: Int {
        get { value }
        set { value = min(max(newValue, range.lowerBound), range.upperBound) }
    }
}

struct Player {
    @Clamped(0...100) var health: Int = 150
}

let player = Player()
print(player.health)
A100
B150
CRuntime error
D0
Attempts:
2 left
💡 Hint
Think about how the initial value is adjusted to fit the allowed range.
Predict Output
intermediate
2:00remaining
Effect of changing wrappedValue in a configured property wrapper
What will be printed after modifying the wrappedValue in this Swift code?
Swift
import Foundation

@propertyWrapper
struct LimitedString {
    private var value: String
    private let maxLength: Int

    init(wrappedValue: String, maxLength: Int) {
        self.maxLength = maxLength
        self.value = String(wrappedValue.prefix(maxLength))
    }

    var wrappedValue: String {
        get { value }
        set { value = String(newValue.prefix(maxLength)) }
    }
}

struct Message {
    @LimitedString(maxLength: 5) var text: String = "HelloWorld"
}

var msg = Message()
msg.text = "SwiftProgramming"
print(msg.text)
A"SwiftProgramming"
B"Hello"
C"Swift"
D"HelloWorld"
Attempts:
2 left
💡 Hint
Check how the setter limits the string length.
🔧 Debug
advanced
2:00remaining
Identify the error in this property wrapper with configuration
What error does this Swift code produce when compiled?
Swift
import Foundation

@propertyWrapper
struct Positive {
    private var value: Int

    init(wrappedValue: Int) {
        if wrappedValue > 0 {
            value = wrappedValue
        }
    }

    var wrappedValue: Int {
        get { value }
        set { value = newValue > 0 ? newValue : 1 }
    }
}

struct Account {
    @Positive var balance: Int = -10
}

let acc = Account()
print(acc.balance)
APrints -10
BRuntime error: Index out of range
CPrints 1
DCompile-time error: 'value' used before being initialized
Attempts:
2 left
💡 Hint
Check if all paths in init assign 'value'.
Predict Output
advanced
2:00remaining
Output of a property wrapper with projectedValue configuration
What is the output of this Swift code using a property wrapper with projectedValue?
Swift
import Foundation

@propertyWrapper
struct Logged<Value> {
    private var value: Value
    var projectedValue: String = ""

    init(wrappedValue: Value) {
        self.value = wrappedValue
        self.projectedValue = "Initialized with \(wrappedValue)"
    }

    var wrappedValue: Value {
        get { value }
        set {
            value = newValue
            projectedValue = "Changed to \(newValue)"
        }
    }
}

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

var config = Config()
config.setting = 20
print(config.$setting)
A"Changed to 20"
B"Initialized with 10"
C20
DRuntime error
Attempts:
2 left
💡 Hint
Look at when projectedValue changes.
Predict Output
expert
3:00remaining
Final value after multiple property wrapper assignments with configuration
What is the final printed value of 'score' after all assignments in this Swift code?
Swift
import Foundation

@propertyWrapper
struct ScoreLimiter {
    private var value: Int
    private let minScore: Int
    private let maxScore: Int

    init(wrappedValue: Int, min: Int, max: Int) {
        self.minScore = min
        self.maxScore = max
        self.value = min(max(wrappedValue, min), max)
    }

    var wrappedValue: Int {
        get { value }
        set { value = min(max(newValue, minScore), maxScore) }
    }
}

struct Game {
    @ScoreLimiter(min: 0, max: 50) var score: Int = 60
}

var game = Game()
game.score = -10
game.score = 30
game.score = 100
print(game.score)
A30
B50
C60
D0
Attempts:
2 left
💡 Hint
Remember the property wrapper clamps values on assignment.