An <code>@propertyWrapper</code> is a special structure or class that adds extra behavior to properties, like controlling how values are stored or validated, making code cleaner and reusable.
Click to reveal answer
beginner
How do you declare a property wrapper in Swift?
You declare a property wrapper by creating a struct or class with the <code>@propertyWrapper</code> attribute and defining a <code>wrappedValue</code> property that manages the stored value.
Click to reveal answer
beginner
What is the role of the wrappedValue property inside a property wrapper?
The wrappedValue property holds the actual value of the property and controls how it is accessed and modified when using the wrapper.
Click to reveal answer
intermediate
Can a property wrapper have an initializer? Why?
Yes, a property wrapper can have an initializer to set up initial values or configure behavior when the wrapper is created, allowing customization of how the wrapped property behaves.
Click to reveal answer
beginner
How do you apply a property wrapper to a property in Swift?
You apply a property wrapper by placing its name with @ before the property declaration, like @MyWrapper var myProperty, which automatically uses the wrapper's logic.
Click to reveal answer
What keyword is used to declare a property wrapper in Swift?
A@property
B@propertyWrapper
C@wrapper
D@wrap
✗ Incorrect
The correct keyword to declare a property wrapper is @propertyWrapper.
Which property must a property wrapper define to manage the wrapped value?
AstoredValue
Bvalue
CpropertyValue
DwrappedValue
✗ Incorrect
The property wrapper must define a wrappedValue property to manage the wrapped value.
How do you apply a property wrapper named Trimmed to a string property?
A@Trimmed var name: String
Bvar @Trimmed name: String
Cvar name: @Trimmed String
DTrimmed var name: String
✗ Incorrect
You apply it by writing @Trimmed var name: String before the property.
Can a property wrapper have parameters in its initializer?
ANo, initializers are not allowed
BOnly default initializers are allowed
CYes, to customize behavior
DOnly static initializers are allowed
✗ Incorrect
Property wrappers can have initializers with parameters to customize how they work.
What happens when you access a property wrapped by a property wrapper?
AThe wrapper's <code>wrappedValue</code> getter is called
BThe property is accessed directly without wrapper
CThe wrapper's initializer runs every time
DThe property is copied to a new variable
✗ Incorrect
Accessing the property calls the wrapper's wrappedValue getter to control the value.
Explain how to create and use a simple property wrapper in Swift.
Think about how you add extra behavior to a property.
You got /4 concepts.
Describe the purpose of the wrappedValue property inside a property wrapper.
It acts like a gatekeeper for the property's value.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of using @propertyWrapper in Swift?
easy
A. To define functions that run asynchronously
B. To create new data types like classes or structs
C. To add reusable behavior to variables by controlling their access and storage
D. To handle errors in Swift code
Solution
Step 1: Understand what @propertyWrapper does
@propertyWrapper is used to add reusable behavior to variables, such as validation or transformation, by controlling how their values are stored and accessed.
Step 2: Compare with other options
Options B, C, and D describe unrelated Swift features: defining types, async functions, and error handling, which are not the purpose of @propertyWrapper.
Final Answer:
To add reusable behavior to variables by controlling their access and storage -> Option C
Confusing @propertyWrapper with class or struct definitions
Thinking it handles async code or errors
Believing it creates new data types
2. Which of the following is the correct minimal syntax to declare a property wrapper named Capitalized?
easy
A. @propertyWrapper struct Capitalized { var wrappedValue: String }
B. struct Capitalized { @propertyWrapper var wrappedValue: String }
C. struct Capitalized { var wrappedValue: String }
D. @propertyWrapper var Capitalized: String
Solution
Step 1: Recall the syntax for declaring a property wrapper
A property wrapper must be declared as a struct or class with the @propertyWrapper attribute before its definition, and it must have a wrappedValue property.
Step 2: Analyze each option
struct Capitalized { var wrappedValue: String } misses the @propertyWrapper attribute. @propertyWrapper struct Capitalized { var wrappedValue: String } correctly uses @propertyWrapper before the struct and defines wrappedValue. struct Capitalized { @propertyWrapper var wrappedValue: String } incorrectly places @propertyWrapper before the property, which is invalid. @propertyWrapper var Capitalized: String tries to declare a variable with @propertyWrapper, which is incorrect syntax.
Final Answer:
@propertyWrapper struct Capitalized { var wrappedValue: String } -> Option A
Quick Check:
Property wrapper syntax = @propertyWrapper before struct [OK]
Hint: Put @propertyWrapper before struct/class, not before property [OK]
Common Mistakes:
Placing @propertyWrapper before the property instead of the struct
Omitting the @propertyWrapper attribute
Trying to declare property wrapper as a variable
3. Given the code below, what will be printed?
@propertyWrapper
struct Trimmed {
var wrappedValue: String
init(wrappedValue: String) {
self.wrappedValue = wrappedValue.trimmingCharacters(in: .whitespaces)
}
}
struct User {
@Trimmed var name: String
}
let user = User(name: " Alice ")
print(user.name)
medium
A. "Alice"
B. " Alice "
C. Compilation error
D. "Alice "
Solution
Step 1: Understand the property wrapper behavior
The Trimmed wrapper trims whitespace from the string when initialized in its init. So the initial value " Alice " is trimmed to "Alice".
Step 2: Check the printed value
The user.name accesses the wrapped value, which is trimmed. So printing user.name outputs "Alice" without spaces.
Final Answer:
"Alice" -> Option A
Quick Check:
Trimmed wrapper removes spaces = "Alice" [OK]
Hint: Property wrapper trims on init, so output is trimmed [OK]
Common Mistakes:
Assuming original spaces remain
Thinking didSet runs before init
Overlooking the trimming in init
4. Identify the error in the following property wrapper declaration:
@propertyWrapper
struct Positive {
var wrappedValue: Int
init(wrappedValue: Int) {
if wrappedValue < 0 {
wrappedValue = 0
}
self.wrappedValue = wrappedValue
}
}
medium
A. No error, code is correct
B. Missing @propertyWrapper attribute
C. wrappedValue property must be a computed property
D. Cannot assign to parameter 'wrappedValue' inside init
Solution
Step 1: Analyze the init parameter usage
In Swift, function parameters are constants and cannot be assigned new values. The line wrappedValue = 0 inside the init tries to assign to the parameter, which causes an error.
Step 2: Check other options
The @propertyWrapper attribute is present, so B is wrong. The wrappedValue can be stored or computed, so C is wrong. The code has an error, so A is wrong.
Final Answer:
Cannot assign to parameter 'wrappedValue' inside init -> Option D
Quick Check:
Function parameters are constants = assignment error [OK]
Hint: Don't assign to init parameters; use a local var instead [OK]
Common Mistakes:
Trying to assign directly to init parameters
Thinking wrappedValue must be computed
Ignoring the @propertyWrapper attribute
5. You want to create a property wrapper Clamped that limits an Int value between a minimum and maximum. Which of the following is the correct way to declare it so that you can use it like @Clamped(min: 0, max: 10) var score: Int?
hard
A. @propertyWrapper struct Clamped {
var wrappedValue: Int
init(min: Int, max: Int) {
wrappedValue = 0
}
}
B. @propertyWrapper struct Clamped {
private var value: Int
let min: Int
let max: Int
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, min), max) }
}
init(wrappedValue: Int, min: Int, max: Int) {
self.min = min
self.max = max
self.value = min(max(wrappedValue, min), max)
}
}
C. @propertyWrapper struct Clamped {
var wrappedValue: Int
init(wrappedValue: Int) {
wrappedValue = 0
}
}
D. @propertyWrapper struct Clamped {
var wrappedValue: Int {
didSet {
if wrappedValue < min { wrappedValue = min }
if wrappedValue > max { wrappedValue = max }
}
}
let min: Int
let max: Int
}
Solution
Step 1: Understand the requirements for Clamped
The wrapper must accept min and max parameters, clamp the wrappedValue between them, and allow usage with @Clamped(min: 0, max: 10).
Step 2: Analyze each option
@propertyWrapper struct Clamped {
private var value: Int
let min: Int
let max: Int
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, min), max) }
}
init(wrappedValue: Int, min: Int, max: Int) {
self.min = min
self.max = max
self.value = min(max(wrappedValue, min), max)
}
} correctly defines min and max as let constants, stores the value privately, clamps in init and setter, and has the correct init signature. @propertyWrapper struct Clamped {
var wrappedValue: Int
init(min: Int, max: Int) {
wrappedValue = 0
}
} lacks wrappedValue initialization and clamping logic. @propertyWrapper struct Clamped {
var wrappedValue: Int
init(wrappedValue: Int) {
wrappedValue = 0
}
} does not accept min and max parameters. @propertyWrapper struct Clamped {
var wrappedValue: Int {
didSet {
if wrappedValue < min { wrappedValue = min }
if wrappedValue > max { wrappedValue = max }
}
}
let min: Int
let max: Int
} tries to clamp in didSet but does not initialize wrappedValue properly and does not clamp in init, which can cause invalid initial values.
Final Answer:
@propertyWrapper struct Clamped {
private var value: Int
let min: Int
let max: Int
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, min), max) }
}
init(wrappedValue: Int, min: Int, max: Int) {
self.min = min
self.max = max
self.value = min(max(wrappedValue, min), max)
}
} -> Option B
Quick Check:
Clamped wrapper needs init with min/max and clamping in setter [OK]
Hint: Use init with wrappedValue and params; clamp in setter and init [OK]
Common Mistakes:
Not accepting min and max in init
Not clamping value on initialization
Trying to clamp only in didSet without proper init
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.