A property wrapper is a way to add extra behavior to properties, like validation or default values, by wrapping the property in a reusable structure.
Click to reveal answer
intermediate
How do you compose multiple property wrappers on a single property in Swift?
You write multiple property wrappers one after another above the property. Swift applies them from the innermost to the outermost wrapper.
Click to reveal answer
intermediate
What happens when you compose property wrappers in Swift?
The property is wrapped first by the innermost wrapper, then each outer wrapper wraps the result, layering behaviors.
Click to reveal answer
intermediate
Show a simple example of composing two property wrappers in Swift.
Example:
@WrapperA
@WrapperB
var value: Int
Here, WrapperB wraps the property first, then WrapperA wraps WrapperB.
Click to reveal answer
beginner
Why use composing property wrappers?
Composing lets you combine multiple behaviors cleanly and reuse them without writing new wrappers for every combination.
Click to reveal answer
In Swift, when you write multiple property wrappers on a property, which wrapper is applied first?
AThe innermost (closest to the property)
BThe outermost (topmost)
CThey are applied simultaneously
DThe compiler decides randomly
✗ Incorrect
Swift applies the innermost wrapper first, then wraps it with the outer wrappers in order.
What is the main benefit of composing property wrappers in Swift?
ATo write less code for functions
BTo make properties slower
CTo avoid using structs
DTo combine multiple behaviors on a property easily
✗ Incorrect
Composing property wrappers allows combining multiple reusable behaviors on a single property.
Which keyword is used to declare a property wrapper in Swift?
A@wrap
B@propertyWrapper
C@wrapper
D@property
✗ Incorrect
The @propertyWrapper attribute declares a struct or class as a property wrapper.
If you have @WrapperA and @WrapperB on a property, which wrapper's wrappedValue is accessed first?
AWrapperA's wrappedValue
BWrapperB's wrappedValue
CBoth at the same time
DNeither
✗ Incorrect
The outermost wrapper (WrapperA) exposes its wrappedValue, which internally accesses WrapperB's wrappedValue.
Can property wrappers in Swift have parameters?
AOnly for classes
BNo, they must be parameterless
CYes, to customize behavior
DOnly for enums
✗ Incorrect
Property wrappers can have parameters to customize how they work.
Explain how composing property wrappers works in Swift and why it is useful.
Think about how layers wrap a gift one after another.
You got /4 concepts.
Describe a simple example where you might want to compose two property wrappers and what each wrapper does.
Imagine one wrapper validates input and another logs changes.
You got /3 concepts.
Practice
(1/5)
1. What does it mean to compose property wrappers in Swift?
easy
A. Apply property wrappers only to functions
B. Use only one property wrapper per property
C. Create a new property wrapper by subclassing another
D. Stack multiple property wrappers on a single property to combine behaviors
Solution
Step 1: Understand property wrappers
Property wrappers add behavior to properties in Swift.
Step 2: Define composition meaning
Composing means stacking multiple wrappers on one property to combine their effects.
Final Answer:
Stack multiple property wrappers on a single property to combine behaviors -> Option D
Quick Check:
Composing = stacking wrappers [OK]
Hint: Remember: composing means stacking wrappers on one property [OK]
Common Mistakes:
Thinking only one wrapper can be used per property
Confusing subclassing with composing
Applying wrappers to functions instead of properties
2. Which of the following is the correct syntax to compose two property wrappers @WrapperA and @WrapperB on a property var value: Int such that @WrapperA is applied first (innermost)?
easy
A. @WrapperB @WrapperA var value: Int
B. @WrapperA @WrapperB var value: Int
C. var @WrapperA @WrapperB value: Int
D. var value: Int @WrapperA @WrapperB
Solution
Step 1: Recall property wrapper order
Property wrappers apply from right to left, so the rightmost is applied first.
Step 2: Check syntax order
Correct syntax places wrappers before the property, so @WrapperB @WrapperA var value: Int applies @WrapperA first, then @WrapperB.
Final Answer:
@WrapperB @WrapperA var value: Int -> Option A
Quick Check:
Right to left wrapper order = @WrapperB @WrapperA var value: Int [OK]
Hint: Remember wrappers apply right to left, so last wrapper is closest to var [OK]
Common Mistakes:
Placing wrappers after the variable name
Reversing the order of wrappers
Using wrappers inside the variable declaration
3. Given these property wrappers:
@propertyWrapper
struct WrapperA {
var wrappedValue: Int
init(wrappedValue: Int) { self.wrappedValue = wrappedValue + 1 }
}
@propertyWrapper
struct WrapperB {
var wrappedValue: Int
init(wrappedValue: Int) { self.wrappedValue = wrappedValue * 2 }
}
struct Example {
@WrapperA @WrapperB var value: Int = 3
}
What is the value of Example().value?
medium
A. 7
B. 8
C. 6
D. 10
Solution
Step 1: Apply wrappers right to left
First, @WrapperB applies: 3 * 2 = 6.
Step 2: Apply @WrapperA next
Then @WrapperA adds 1: 6 + 1 = 7.
Final Answer:
7 -> Option A
Quick Check:
(3 * 2) + 1 = 7 [OK]
Hint: Apply wrappers from right to left to find final value [OK]
Common Mistakes:
Applying wrappers left to right
Forgetting to add 1 in WrapperA
Multiplying after adding instead of before
4. Consider this code snippet:
@propertyWrapper
struct WrapperX {
var wrappedValue: Int
init(wrappedValue: Int) { self.wrappedValue = wrappedValue + 5 }
}
struct Test {
@WrapperX @WrapperX var number: Int = 10
}
What is the error in this code?
medium
A. Cannot apply the same property wrapper twice on one property
B. The property wrapper's init is called twice, causing unexpected behavior
C. No error; code compiles and runs correctly
D. The property wrapper must have a default initializer
Solution
Step 1: Check if multiple same wrappers allowed
Swift allows stacking the same property wrapper multiple times.
Step 2: Analyze wrapper initialization
Each wrapper adds 5, so first adds 5 to 10 = 15, second adds 5 to 15 = 20.
Final Answer:
No error; code compiles and runs correctly -> Option C
Quick Check:
Same wrapper stacking allowed = No error; code compiles and runs correctly [OK]
Hint: Same wrapper can be stacked; no syntax error [OK]
Common Mistakes:
Assuming duplicate wrappers cause errors
Expecting default initializer requirement
Confusing runtime behavior with compile errors
5. You want to create a property that is both clamped between 0 and 100 and logged when changed. You have two property wrappers:
@propertyWrapper
struct Clamped {
private var value: Int
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)
}
}
@propertyWrapper
struct Logged {
private var value: Int
var wrappedValue: Int {
get { value }
set {
print("Value changed to \(newValue)")
value = newValue
}
}
init(wrappedValue: Int) {
self.value = wrappedValue
}
}
struct Settings {
@Logged @Clamped(0...100) var score: Int = 150
}
What will be printed and what is the final value of Settings().score?
hard
A. Prints 'Value changed to 150', final value 150
B. Prints nothing, final value 100
C. Prints 'Value changed to 100', final value 100
D. Prints 'Value changed to 0', final value 0
Solution
Step 1: Apply wrappers right to left
First, @Clamped(0...100) clamps 150 to 100 in init.
Step 2: Then @Logged receives 100
@Logged init sets value=100 directly (no setter called, no print).
Final Answer:
Prints nothing, final value 100 -> Option B
Quick Check:
Clamp then direct init set = no print, value 100 [OK]
Hint: Initial values chain through inits right to left; no setter/print on init [OK]
Common Mistakes:
Thinking log prints during initialization
Thinking clamp applies after log
Assuming propagation calls inner setters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.