A property wrapper is a way to add extra behavior to properties, like adding validation or default values, by wrapping the property in a reusable structure.
Click to reveal answer
intermediate
How do you add configuration to a property wrapper?
You add configuration by defining parameters in the property wrapper's initializer, allowing customization when applying the wrapper to a property.
Click to reveal answer
beginner
Explain the role of the wrappedValue property in a property wrapper.
The wrappedValue property holds the actual value of the property being wrapped. It controls how the value is stored and accessed.
Click to reveal answer
intermediate
Can a property wrapper have multiple configuration parameters? Give an example.
Yes, a property wrapper can have multiple parameters. For example, a Clamped wrapper might take min and max values to limit the property's range.
Click to reveal answer
intermediate
What happens if you don’t provide a default value for a property wrapper’s parameter?
You must provide the parameter value when using the wrapper, otherwise the code won’t compile because the wrapper’s initializer requires it.
Click to reveal answer
What keyword do you use to define a property wrapper in Swift?
A@wrapper
B@property
C@wrap
D@propertyWrapper
✗ Incorrect
The correct keyword to define a property wrapper is @propertyWrapper.
How do you access the value inside a property wrapper?
AUsing the <code>property</code> keyword
BUsing the <code>value</code> property
CUsing the <code>wrappedValue</code> property
DDirectly without any property
✗ Incorrect
The wrappedValue property is used to get or set the wrapped property's value.
How can you customize a property wrapper when applying it to a property?
ABy passing parameters to the wrapper’s initializer
BBy changing the property’s type
CBy using a different property name
DBy adding a protocol conformance
✗ Incorrect
You customize a property wrapper by passing parameters to its initializer when you apply it.
Which of these is a valid way to declare a property wrapper with a configuration parameter?
Hint: Look for @propertyWrapper + init with config param [OK]
Common Mistakes:
Missing @propertyWrapper attribute
No initializer with config parameter
Using functions instead of init for config
3. Consider this Swift code using a property wrapper with configuration:
@propertyWrapper
struct Clamped {
private var value: Int
private let range: ClosedRange<Int>
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, range.lowerBound), range.upperBound) }
}
init(wrappedValue: Int, range: ClosedRange<Int>) {
self.range = range
self.value = min(max(wrappedValue, range.lowerBound), range.upperBound)
}
}
struct Example {
@Clamped(range: 1...10) var number: Int = 5
}
var ex = Example()
ex.number = 15
print(ex.number)
What is the output when this code runs?
medium
A. 10
B. 5
C. 15
D. 1
Solution
Step 1: Understand the Clamped wrapper behavior
The wrapper clamps the value between the given range (1 to 10).
Step 2: Analyze the assignment and print
Assigning 15 triggers clamping to upper bound 10, so print outputs 10.
Final Answer:
10 -> Option A
Quick Check:
Value clamped to 10 [OK]
Hint: Remember clamping limits values to range [OK]
Common Mistakes:
Expecting original value 15 to print
Confusing initial value 5 with final
Ignoring clamping logic in setter
4. This Swift code attempts to use a property wrapper with configuration but causes a compile error:
@propertyWrapper
struct Limited {
var wrappedValue: Int
let max: Int
init(max: Int) {
self.max = max
self.wrappedValue = 0
}
}
struct Data {
@Limited(max: 100) var score: Int = 50
}
What is the main reason for the error?
medium
A. Cannot assign default value to score when using wrapper
B. Missing init(wrappedValue:max:) initializer to accept initial value
When using a default value on a wrapped property, the wrapper needs an initializer init(wrappedValue:max:) to accept it.
Step 2: Identify missing initializer
The code only has init(max:), so compiler cannot match score: Int = 50.
Final Answer:
Missing init(wrappedValue:max:) initializer to accept initial value -> Option B
Quick Check:
Init must accept wrappedValue when default given [OK]
Hint: Add init(wrappedValue:..., config...) for default values [OK]
Common Mistakes:
Thinking stored properties are disallowed
Believing default value assignment is invalid
Confusing let/var for causing error
5. You want to create a property wrapper Validated that checks a string against a minimum length and a regular expression pattern, both configurable. Which approach correctly supports this configuration and usage?
@propertyWrapper
struct Validated {
private var value: String
private let minLength: Int
private let pattern: String
var wrappedValue: String {
get { value }
set {
if newValue.count >= minLength && newValue.range(of: pattern, options: .regularExpression) != nil {
value = newValue
} else {
value = ""
}
}
}
// What initializer should be here?
}
struct User {
@Validated(minLength: 5, pattern: "^[a-zA-Z]+$") var username: String = ""
}