0
0
Swiftprogramming~20 mins

@propertyWrapper declaration 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 code using @propertyWrapper?
Consider the following Swift code that uses a property wrapper. What will be printed when running this code?
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
Look at how the setter modifies the string using the capitalized property.
Predict Output
intermediate
2:00remaining
What error does this Swift @propertyWrapper code produce?
Examine this Swift code using @propertyWrapper. What error will it produce when compiled?
Swift
@propertyWrapper
struct NumberWrapper {
    var wrappedValue: Int

    init() {
        wrappedValue = 0
    }
}

struct Data {
    @NumberWrapper var number: Int
}

let d = Data()
AError: Cannot assign to property: 'wrappedValue' is a get-only property
BError: Property wrapper must have an initial value
CNo error, compiles successfully
DError: Missing argument for parameter 'wrappedValue' in call
Attempts:
2 left
💡 Hint
Check how the property wrapper is initialized and what initializers are available.
🔧 Debug
advanced
2:00remaining
Why does this @propertyWrapper code cause a runtime crash?
This Swift code compiles but crashes at runtime. What is the cause of the crash?
Swift
@propertyWrapper
struct Positive {
    private var value: Int
    var wrappedValue: Int {
        get { value }
        set {
            guard newValue > 0 else {
                fatalError("Value must be positive")
            }
            value = newValue
        }
    }

    init(wrappedValue: Int) {
        self.wrappedValue = wrappedValue
    }
}

struct Account {
    @Positive var balance: Int
}

let a = Account(balance: -10)
AThe code crashes because value is not initialized before use
BThe fatalError is triggered because the initial wrappedValue is negative
CThe code crashes due to infinite recursion in the setter
DNo crash occurs; the code runs fine
Attempts:
2 left
💡 Hint
Look at how the initial wrappedValue is assigned and how the setter guard works.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a @propertyWrapper with projectedValue?
Choose the correct Swift code that declares a @propertyWrapper with a projectedValue property.
A
@propertyWrapper
struct Wrapper {
    var wrappedValue: Int
    var projectedValue: String {
        "Value is \(wrappedValue)"
    }
}
B
@propertyWrapper
struct Wrapper {
    var wrappedValue: Int
    var projectedValue: Int {
        wrappedValue * 2
    }
}
C
@propertyWrapper
struct Wrapper {
    var wrappedValue: Int
    var projectedValue: Int {
        get { wrappedValue * 2 }
    }
}
D
@propertyWrapper
struct Wrapper {
    var wrappedValue: Int
    var projectedValue: Int {
        set { wrappedValue = newValue }
    }
}
Attempts:
2 left
💡 Hint
The projectedValue must be a readable property, usually with a getter.
🚀 Application
expert
2:00remaining
How many items are in the resulting array after using this @propertyWrapper in a loop?
Given this Swift code using a @propertyWrapper that tracks assignments, how many elements will be in the assignments array after the loop?
Swift
@propertyWrapper
class Tracker {
    private(set) var assignments: [Int] = []
    var wrappedValue: Int {
        didSet {
            assignments.append(wrappedValue)
        }
    }
    init(wrappedValue: Int) {
        self.wrappedValue = wrappedValue
    }
}

struct Data {
    @Tracker var value: Int
}

var data = Data(value: 0)
for i in 1...3 {
    data.value = i
}

let count = data.$value.assignments.count
print(count)
A4
B3
C1
D0
Attempts:
2 left
💡 Hint
Remember the initial assignment in init and the didSet calls in the loop.