A property wrapper is a Swift feature that lets you add extra behavior to properties, like validation or formatting, by wrapping the property’s value with custom code.
Click to reveal answer
intermediate
How do you define a custom validation property wrapper?
You create a struct or class with the @propertyWrapper attribute, include a wrappedValue property, and add validation logic inside the setter of wrappedValue.
Click to reveal answer
intermediate
What happens if a value fails validation in a custom property wrapper?
You can handle it by rejecting the value, setting a default, throwing an error, or logging a message, depending on how you design the wrapper.
Click to reveal answer
beginner
Show a simple example of a custom validation property wrapper that ensures a string is not empty.
@propertyWrapper
struct NonEmpty {
private var value: String
init(wrappedValue: String) {
self.value = wrappedValue.isEmpty ? "Default" : wrappedValue
}
var wrappedValue: String {
get { value }
set { value = newValue.isEmpty ? "Default" : newValue }
}
}
struct User {
@NonEmpty var name: String
}
Click to reveal answer
beginner
Why use custom validation property wrappers instead of validating manually?
They keep your code clean by centralizing validation logic, reduce repeated code, and make properties safer and easier to maintain.
Click to reveal answer
What keyword do you use to mark a custom property wrapper in Swift?
A@validate
B@wrapper
C@custom
D@propertyWrapper
✗ Incorrect
The correct keyword to define a property wrapper is @propertyWrapper.
Where do you put the validation logic inside a custom property wrapper?
AInside the init method only
BInside the wrappedValue setter
COutside the property wrapper
DInside the wrappedValue getter
✗ Incorrect
Validation logic is usually placed inside the setter of wrappedValue to check or modify new values.
What does the wrappedValue property represent in a property wrapper?
AThe actual value being wrapped
BThe name of the property
CThe type of the property
DThe validation function
✗ Incorrect
wrappedValue holds the actual value that the property wrapper manages.
If you want to provide a default value when validation fails, where do you do it?
AIn the wrappedValue setter
BIn the property declaration only
CIn the getter of wrappedValue
DIn the main struct using the property wrapper
✗ Incorrect
You provide default values or fallback logic inside the setter of wrappedValue when validation fails.
Which of these is NOT a benefit of using custom validation property wrappers?
ACentralizing validation logic
BReducing repeated code
CAutomatically fixing all bugs
DMaking properties safer
✗ Incorrect
Property wrappers help with validation but do not automatically fix all bugs.
Explain how to create a custom validation property wrapper in Swift and why it is useful.
Think about how you can check or change values when they are set.
You got /4 concepts.
Describe a simple example of a custom validation property wrapper that ensures a string is never empty.
Imagine you want to avoid empty names in a user profile.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a custom validation property wrapper in Swift?
easy
A. To automatically check and enforce rules on property values
B. To change the type of a property at runtime
C. To make properties private and inaccessible
D. To store multiple values in a single property
Solution
Step 1: Understand property wrappers
Property wrappers in Swift add extra behavior to properties, such as validation.
Step 2: Identify validation role
Custom validation wrappers check if values meet certain rules before storing them.
Final Answer:
To automatically check and enforce rules on property values -> Option A
Quick Check:
Validation = enforce rules [OK]
Hint: Validation wrappers enforce rules on data automatically [OK]
Common Mistakes:
Thinking wrappers change property type
Confusing validation with access control
Assuming wrappers store multiple values
2. Which of the following is the correct syntax to declare a custom validation property wrapper named NonEmpty for a String property in Swift?
easy
A. var name: @NonEmpty String
B. var @NonEmpty name: String
C. @NonEmpty var name: String
D. NonEmpty var name: String
Solution
Step 1: Recall property wrapper syntax
In Swift, property wrappers are applied with @ before the wrapper name, placed before the variable declaration.
Step 2: Match correct syntax
Correct syntax is @WrapperName var propertyName: Type, so @NonEmpty var name: String is correct.
Final Answer:
@NonEmpty var name: String -> Option C
Quick Check:
@WrapperName before var = correct syntax [OK]
Hint: Use @ before wrapper name, then var property [OK]
Common Mistakes:
Placing @ after var
Putting wrapper inside type
Omitting @ symbol
3. Consider this Swift code using a custom validation property wrapper Positive that only allows positive integers:
@propertyWrapper
struct Positive {
private var value: Int = 1
var wrappedValue: Int {
get { value }
set { value = newValue > 0 ? newValue : 1 }
}
}
struct Account {
@Positive var balance: Int
}
let acc = Account()
print(acc.balance)
What will be printed when this code runs?
medium
A. 1
B. 0
C. nil
D. Compilation error
Solution
Step 1: Understand default value in wrapper
The wrapper's private value starts at 1, and sets new values only if positive; else resets to 1.
Step 2: Check initial property value
Account's balance uses @Positive but no initial value is set, so it uses wrapper's default 1.
Final Answer:
1 -> Option A
Quick Check:
Default wrapper value = 1 [OK]
Hint: Default wrapper value used if no initial value given [OK]
Common Mistakes:
Expecting 0 as default
Assuming nil is possible
Thinking code won't compile
4. This Swift code attempts to create a custom validation property wrapper NonNegative but has an error:
@propertyWrapper
struct NonNegative {
var value: Int
var wrappedValue: Int {
get { value }
set { if newValue >= 0 { value = newValue } }
}
}
struct Score {
@NonNegative var points: Int = -5
}
What is the main problem causing this code to fail?
medium
A. wrappedValue setter does not handle negative values correctly
B. Property wrapper lacks an initializer to set initial value
C. Cannot assign default value to property with wrapper
D. Missing @ symbol before property wrapper
Solution
Step 1: Check property wrapper requirements
Property wrappers must have an init(wrappedValue:) to accept initial values.
Step 2: Identify missing initializer
NonNegative lacks init(wrappedValue:) so compiler cannot assign -5 to points.
Final Answer:
Property wrapper lacks an initializer to set initial value -> Option B
Quick Check:
Missing init(wrappedValue:) = error [OK]
Hint: Add init(wrappedValue:) to accept initial values [OK]
Common Mistakes:
Ignoring missing initializer
Blaming setter logic
Thinking default value assignment is invalid
5. You want to create a custom validation property wrapper LimitedLength that limits a String property's length to a maximum number of characters. Which of the following implementations correctly enforces this limit and allows setting the max length when declaring the property?
@propertyWrapper
struct LimitedLength {
private var value: String = ""
private let maxLength: Int
var wrappedValue: String {
get { value }
set {
if newValue.count <= maxLength {
value = newValue
} else {
value = String(newValue.prefix(maxLength))
}
}
}
// Which initializer is correct?
???
}
struct User {
@LimitedLength(maxLength: 5) var username: String
}
hard
A. init(maxLength: Int) { self.maxLength = maxLength }