Property Wrappers in Swift — Part 1

DevTechie Inc
Apr 6, 2022
Property wrappers were introduced in Swift 5.1(WWDC19) and they play a big part in SwiftUI. Property wrappers provide a way to reduce the amount of duplicate code which is involved in writing computed properties or getters and setters for variables to validate or transform their values before storing them within a class and struct.

Let’s understand the problem property wrapper try to solve a little better.

Imagine we have a struct to represent DevTechie course app:

struct Course {
    var name: String = ""
    var author: String = ""
}
we have two properties for Course struct name and author . While storing values in these properties, we want to make sure that our course name and author is capitalized. So in order to put that validation in place we will make properties private and provide getter and setter for these private properties. For private variables we will rename them to _name and _author so all external references to those properties don’t change as shown below:

struct Course {
    private var _name: String = ""
    
    var name: String {
        get { _name }
        set { _name = newValue.capitalized }
    }
    
    private var _author: String = ""
    
    var author: String {
        get { _author }
        set { _author = newValue.capitalized }
    }
    
}
Now when course’s name and author properties are set, we capitalize them before storing into our private variables.

var course = Course()
course.name = "machine learning in iOS"
course.author = "devTechie"print(course.name, ", By - ", course.author)
Our output will show

Machine Learning In Ios , By -  Devtechie
Notice that the code to capitalize the value is repeated for each property. Imagine having 10 or 20 properties in a type, we will find ourselves repeating over and over.

Property wrapper to the rescue 😊

Property wrappers are declared using @propertyWrapper directive, decorated right before class or struct implementation. Property wrappers must include a property called wrappedValue which is a computed property and has getter and setter code.

@propertyWrapper
struct CapitalCase {
    private var value: String = ""
    var wrappedValue: String {
        get { value }
        set { value = newValue.capitalized() }
    }
    init(wrappedValue initialValue: String) {
        self.wrappedValue = initialValue
    }
}
Once our property wrapper is in place, we can decorate our course properties with this newly created property wrapper.

struct Course {
    @CapitalCase var name: String = ""
    @CapitalCase var author: String = ""
}
Our example usage remain same:

var course = Course()
course.name = "machine learning in iOS"
course.author = "devTechie"print(course.name, ", By - ", course.author)
Our output will also remain same:

Machine Learning In Ios , By -  Devtechie
Next article will focus on accepting multiple values for property wrappers.

With that, we have reached the end of this article. Thank you once again for reading, if you liked it, don’t forget to subscribe our newsletter.