Do some processing about the wrapped variable
Example:
import Foundation
import SwiftUI
@propertyWrapper struct Capitalized {
var wrappedValue: String {
didSet { wrappedValue = wrappedValue.capitalized }
}
init(wrappedValue: String) {
self.wrappedValue = wrappedValue.capitalized
}
}
struct User {
@Capitalized var firstName: String
@Capitalized var lastName: String
}
// John Appleseed
var user = User(firstName: "john", lastName: "appleseed")
// John Sundell
user.lastName = "sundell"
print(user.lastName)
Point 2: Property wrappers can also have properties of their own
import Foundation
import SwiftUI
@propertyWrapper struct Capitalized {
let prefix = "Fencao Culture - "
var wrappedValue: String {
didSet { wrappedValue = prefix + wrappedValue.capitalized }
}
init(wrappedValue: String) {
self.wrappedValue = prefix + wrappedValue.capitalized
}
}
struct User {
@Capitalized var firstName: String
@Capitalized var lastName: String
}
// John Appleseed
var user = User(firstName: "john", lastName: "appleseed")
// John Sundell
user.lastName = "sundell"
print(user.lastName)