Property Wrappers in Swift — Part 2

DevTechie Inc
Apr 6, 2022
Previous article covered basics and need for property wrappers. In this article, we will focus on accepting and supporting multiple properties for property wrappers.

In this example, we will design a property wrapper which will allow us to add discount to DevTechie online video course sale. As we know that, discount value can’t be below 1%(no point of having discount 😂) and it can’t be above 100% so we will design property wrapper which will constrain discount to be between 1 and 100.

Our property wrapper will be called DevTechieDiscount (aptly named ☺️), it will have two variables to constrain discount to min and max values. We will also have init which will be initialized with min, max values of discount that can be applied on course.

Let’s look at our property wrapper definition:

@propertyWrapper
struct DevTechieDiscount {
    var min: Int
    var max: Int
    var value: Int
    
    init(wrappedValue: Int, min: Int, max: Int) {
        value = wrappedValue
        self.min = min
        self.max = max
    }
    
    var wrappedValue: Int {
        get { value }
        set {
            if newValue > max {
                value = max
            } else if newValue < min {
                value = min
            } else {
                value = newValue
            }
        }
    }
}
Notice wrappedValue definition for set , we are checking if value we are trying to set is more then the defined max then we set value to be the max same goes for min if newValue is smaller then min , we set value to be min and for all other cases where newValue falls between min and max we simply set value to be the newValue.

Let’s look at our Course struct:

struct Course {
    var name: String = ""
    var price: Int = 10
    @DevTechieDiscount(min: 1, max: 50) var discount: Int = 10
    
    func getFinalPrice() {
        print("Final price: ", price - price * discount / 100)
    }
}
Take a look at discount variable and how its decorated with DevTechieDiscount property wrapper which has min and maxdefined, meaning min discount that can be applied will be 1% and max discount that can be applied will be 50%.

We also have a function called getFinalPrice, which will print price after discount has been applied.

Lets create course object with values for name, price and discount.

var course = Course()
course.name = "SwiftUI in Depth"
course.price = 30
course.discount = 10course.getFinalPrice()
getFinalPrice will print following:

Final price:  27
So our discount is computed as 30–30*10/100.

Lets try to set discount value to more then the max allowed value.

var course1 = Course()
course1.name = "SwiftUI 3"
course1.price = 50
course1.discount = 100course1.getFinalPrice()
This will print following:

Final price:  25
Discount applied is still 50%, which is the max defined by DevTechieDiscount property wrapper.

Hope you enjoyed this example of property wrapper. We will cover more in next article.

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.