An introduction to one of Swift 5.1’s powerful new features: Property Wrappers. We implement a property wrapper to improve a common pattern we’re faced with when writing unit tests.
Chip sent the Apps Team to NSSpain last week. All of the talks were excellent but my personal favourite was “Property Wrappers or How Swift decided to become Java” by Vincent Pradeilles.
In his talk Vincent introduced a new feature in Swift 5.1 that perhaps got overshadowed in the excitement of SwiftUI and Combine: property wrappers.
Property wrappers are comparable to Kotlin’s annotations. Simply put, property wrappers encapsulate read and write access to a property while adding extra behaviour. We might use a property wrapper, for example, to read and persist values to UserDefaults when accessed.