0
0
Swiftprogramming~20 mins

WrappedValue and projectedValue 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 Swift property wrapper example?
Consider this Swift code using a property wrapper. What will be printed when accessing user.name and user.$name?
Swift
import Foundation

@propertyWrapper
struct Capitalized {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized }
    }
    var projectedValue: String {
        "Projected: \(value)"
    }
}

struct User {
    @Capitalized var name: String
}

var user = User()
user.name = "john doe"
print(user.name)
print(user.$name)
A
John Doe
Projected: John Doe
B
john doe
Projected: John Doe
C
John Doe
john doe
D
john doe
Projected: john doe
Attempts:
2 left
💡 Hint
Remember that wrappedValue applies capitalization, and projectedValue returns a string with prefix.
🧠 Conceptual
intermediate
1:30remaining
What does the projectedValue property provide in a Swift property wrapper?
In Swift property wrappers, what is the main purpose of the projectedValue property?
AIt replaces the wrappedValue property for reading and writing.
BIt stores the original unmodified value before wrapping.
CIt automatically validates the wrapped value before setting it.
DIt provides an alternative interface to access additional functionality related to the wrapped property.
Attempts:
2 left
💡 Hint
Think about how you access the property with a $ prefix.
🔧 Debug
advanced
2:30remaining
Why does this Swift code cause a compile error?
Examine this property wrapper code. Why does it cause a compile error? @propertyWrapper struct Wrapper { var wrappedValue: Int var projectedValue: Int { wrappedValue * 2 } } struct Example { @Wrapper var number: Int } let e = Example(number: 5) print(e.$number)
Swift
import Foundation

@propertyWrapper
struct Wrapper {
    var wrappedValue: Int
    var projectedValue: Int {
        wrappedValue * 2
    }
}

struct Example {
    @Wrapper var number: Int
}

let e = Example(number: 5)
print(e.$number)
ABecause the struct Example does not declare number as optional.
BBecause the property wrapper lacks an initializer to set wrappedValue.
CBecause wrappedValue cannot be an Int type.
DBecause projectedValue must be a function, not a computed property.
Attempts:
2 left
💡 Hint
Property wrappers need an initializer if wrappedValue is not optional or has no default.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a projectedValue in a Swift property wrapper?
Choose the correct syntax for defining a projectedValue property in a Swift property wrapper that returns a String.
Avar projectedValue: String { "Value is \(wrappedValue)" }
Bfunc projectedValue() -> String { return "Value is \(wrappedValue)" }
Cvar projectedValue: String() { "Value is \(wrappedValue)" }
Dlet projectedValue: String = "Value is \(wrappedValue)"
Attempts:
2 left
💡 Hint
projectedValue is a computed property, not a method or constant.
🚀 Application
expert
3:00remaining
How many items are in the dictionary after this Swift code runs?
Given this Swift property wrapper and usage, how many key-value pairs are in the dictionary counts after execution?
Swift
@propertyWrapper
struct Counted {
    private static var counts = [String: Int]()
    private let key: String
    var wrappedValue: String {
        didSet {
            Self.counts[key, default: 0] += 1
        }
    }
    var projectedValue: Int {
        Self.counts[key] ?? 0
    }
    init(wrappedValue: String, key: String) {
        self.wrappedValue = wrappedValue
        self.key = key
        Self.counts[key] = 0
    }
}

struct Test {
    @Counted(key: "a") var first = "Hello"
    @Counted(key: "b") var second = "World"
}

var t = Test()
t.first = "Hi"
t.second = "Earth"
t.first = "Hey"
A4
B3
C2
D1
Attempts:
2 left
💡 Hint
Each key is unique and increments count on each wrappedValue change.