What is the output of the following Swift code using two composed property wrappers?
import Foundation @propertyWrapper struct Trimmed { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.trimmingCharacters(in: .whitespacesAndNewlines) } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } } @propertyWrapper struct Uppercased { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.uppercased() } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } } struct User { @Trimmed @Uppercased var name: String } var user = User(name: " swift ") print(user.name)
Remember the order in which property wrappers are applied matters.
The property wrappers are applied from the closest to the property outward. Here, @Uppercased is applied first, converting the string to uppercase, then @Trimmed trims whitespace. So the final output is "SWIFT".
What will be the value of settings.title after this code runs?
@propertyWrapper struct Capitalized { private var value: String = "" var wrappedValue: String { get { value } set { value = newValue.capitalized } } init(wrappedValue: String) { self.wrappedValue = wrappedValue } } @propertyWrapper struct Suffix { private var value: String = "" private let suffix: String var wrappedValue: String { get { value } set { value = newValue + suffix } } init(wrappedValue: String, suffix: String) { self.suffix = suffix self.wrappedValue = wrappedValue } } struct Settings { @Suffix(suffix: "!") @Capitalized var title: String } var settings = Settings(title: "hello world") print(settings.title)
Check the order of wrappers and how each modifies the string.
The @Capitalized wrapper capitalizes the string first, then @Suffix adds an exclamation mark. So the final value is "Hello World!".
What error does this Swift code produce when compiling?
@propertyWrapper struct DoubleValue { var wrappedValue: Int { didSet { wrappedValue *= 2 } } init(wrappedValue: Int) { self.wrappedValue = wrappedValue } } @propertyWrapper struct Increment { var wrappedValue: Int { get { value } set { value = newValue + 1 } } private var value: Int = 0 init(wrappedValue: Int) { self.wrappedValue = wrappedValue } } struct Counter { @DoubleValue @Increment var count: Int } let c = Counter(count: 3)
Look at the setter of DoubleValue and what happens when it sets wrappedValue.
The setter of DoubleValue modifies wrappedValue inside its own setter, causing infinite recursion and a compile-time error.
Which of the following Swift code snippets correctly composes two property wrappers where one has a parameter?
Remember how to apply parameters to property wrappers and their order.
Option D correctly applies @Suffix(suffix: "!") with a parameter and composes it with @Trimmed. Other options misuse parameters or order.
Given these two property wrappers and their composition, how many times is the wrappedValue setter called when setting example.value = 5?
@propertyWrapper struct Logger { var wrappedValue: Int { didSet { print("Logger set to \(wrappedValue)") } } init(wrappedValue: Int) { self.wrappedValue = wrappedValue } } @propertyWrapper struct Doubler { var wrappedValue: Int { get { value } set { value = newValue * 2 print("Doubler set to \(value)") } } private var value: Int init(wrappedValue: Int) { self.value = wrappedValue * 2 print("Doubler init with \(value)") } } struct Example { @Logger @Doubler var value: Int } var example = Example(value: 3) example.value = 5
Trace the setter calls from the outer wrapper to the inner wrapper.
Setting example.value = 5 calls the outer @Logger setter once, which sets the inner @Doubler wrappedValue once. So total setter calls are 2.