Creating a Custom Property Wrapper
Implementing @propertyWrapper with wrappedValue to encapsulate logic.
Welcome
Property wrappers encapsulate property storage logic, letting you reuse boilerplate like validation, transformation, or persistence.
Basic Syntax
```swift
@propertyWrapper
struct Clamped {
var wrappedValue: Int { didSet { wrappedValue = min(max(0,wrappedValue),100) } }
init(wrappedValue: Int) { self.wrappedValue = min(max(0,wrappedValue),100) }
}
struct Gauge {
@Clamped var level = 50
}
var g = Gauge()
g.level = 150 // clamped to 100
```
All lessons in this course
- Creating a Custom Property Wrapper
- Projected Values with $binding
- @Published and Observation in Combine
- Composing Multiple Property Wrappers