Property wrappers help you write less repeated code by handling common tasks automatically for your properties.
Why property wrappers reduce boilerplate in Swift
@propertyWrapper struct WrapperName { private var value: Type var wrappedValue: Type { get { value } set { value = newValue } } init(wrappedValue: Type) { self.value = wrappedValue } }
The @propertyWrapper keyword defines a reusable wrapper around a property.
The wrappedValue property controls how the wrapped property behaves.
@propertyWrapper struct Capitalized { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.capitalized } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } }
@Capitalized wrapper reduces the need to write capitalization code every time.struct User { @Capitalized var name: String } var user = User(name: "john") print(user.name) // Prints "John"
This program uses a property wrapper to automatically remove spaces around the text, so you don't have to write trimming code each time.
@propertyWrapper struct Trimmed { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.trimmingCharacters(in: .whitespacesAndNewlines) } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } } struct Message { @Trimmed var text: String } var msg = Message(text: " Hello Swift! ") print("'\(msg.text)'" )
Property wrappers keep your code DRY (Don't Repeat Yourself) by centralizing common logic.
You can reuse property wrappers across many properties and types.
They make your code easier to maintain and understand.
Property wrappers reduce repeated code by wrapping common property behaviors.
They help keep your code clean and consistent.
Using them makes adding features to properties simple and reusable.
Practice
Solution
Step 1: Understand property wrappers purpose
Property wrappers let you write common property behavior once and reuse it.Step 2: Identify how boilerplate is reduced
By wrapping repeated code for properties, you avoid rewriting the same logic multiple times.Final Answer:
They encapsulate common property logic to reuse it easily -> Option DQuick Check:
Property wrappers = reuse common logic [OK]
- Confusing property wrappers with UI features
- Thinking property wrappers remove type declarations
- Believing property wrappers replace functions
Solution
Step 1: Recall property wrapper declaration syntax
Property wrappers use the attribute @propertyWrapper before a struct or class.Step 2: Check the correct syntax details
@propertyWrapper struct Wrapper { var wrappedValue: Int } correctly uses @propertyWrapper and defines wrappedValue with a type and var keyword.Final Answer:
@propertyWrapper struct Wrapper { var wrappedValue: Int } -> Option AQuick Check:
@propertyWrapper + struct + wrappedValue = correct syntax [OK]
- Omitting the @ symbol before propertyWrapper
- Using wrong attribute name like @wrapper
- Missing var keyword for wrappedValue
@propertyWrapper
struct Clamped {
var value: Int
var wrappedValue: Int {
get { value }
set { value = min(max(newValue, 0), 10) }
}
init(wrappedValue: Int) {
self.value = min(max(wrappedValue, 0), 10)
}
}
struct Score {
@Clamped var points: Int
}
var s = Score(points: 15)
print(s.points)
What is the output?Solution
Step 1: Understand the Clamped property wrapper behavior
The wrapper limits values between 0 and 10 using min and max functions.Step 2: Analyze initialization and print output
When Score is created with points = 15, the wrapper clamps it to 10. Printing s.points outputs 10.Final Answer:
10 -> Option CQuick Check:
Clamped value of 15 = 10 [OK]
- Assuming original value 15 is stored without clamping
- Confusing wrappedValue with value property
- Expecting runtime error for out-of-range value
@propertyWrapper
struct Capitalized {
var wrappedValue: String {
didSet { wrappedValue = wrappedValue.capitalized }
}
init(wrappedValue: String) {
self.wrappedValue = wrappedValue.capitalized
}
}
struct Person {
@Capitalized var name: String
}Solution
Step 1: Identify the problem with didSet on wrappedValue
Using didSet on wrappedValue that modifies wrappedValue causes infinite recursion.Step 2: Understand why this causes compile error
The setter triggers didSet, which sets wrappedValue again, looping endlessly and causing error.Final Answer:
Using didSet inside wrappedValue causes infinite recursion -> Option AQuick Check:
didSet modifying wrappedValue = recursion error [OK]
- Thinking struct cannot be property wrapper
- Ignoring recursion caused by didSet setter
- Believing initializer causes error here
Solution
Step 1: Identify how property wrappers reduce repeated code
Property wrappers let you write logging once inside the wrapper's setter.Step 2: Compare options for logging property changes
Create a @Logger wrapper that prints old and new values inside wrappedValue's setter uses a @Logger wrapper to centralize logging, avoiding repeated code in each setter.Final Answer:
Create a @Logger wrapper that prints old and new values inside wrappedValue's setter -> Option BQuick Check:
Use property wrapper to centralize logging [OK]
- Manually adding print in every setter (repeats code)
- Using global functions instead of wrappers
- Trying to override Swift's default setter globally
Ready to prove it?
Pass the test and this pattern moves unknown
