Property Wrappers and SwiftUI

Nilay Keven
3 min readOct 5, 2020

--

At WWDC 2019, Apple introduced incredibly fascinating new technologies such as SwiftUI and the new release of Swift language. We made a small introduction to SwiftUI before. If you haven’t read it, you can access it from the link below.

Swift 5.1 is released with new features that extend the capabilities of the language and standard library. One of the new features which come with Xcode 11 is property wrappers. In this article, we will talk about property wrappers and SwiftUI. First of all, we will take a closer look at what is property wrapper and how we can write our property wrappers.

I want to start with a question, what is property wrapper? 🤔

We used properties in Swift to store some kind of data. Sometimes we need properties that have some kind of logic to be triggered when they’re modified. At this point, property wrappers like Superman lend a hand to eliminate boilerplate code.

What I feel while eliminating the boilerplate code

Let’s take a closer look at property wrappers. Property wrapper is a generic structure that encapsulates a behavior. That behavior is implemented from get and set methods. That feature supplies new opportunities for code reuse and generalization.

How to build our own property wrappers? 🧐

If you want to build a property wrapper, you make a structure, enumeration, or class that defines a wrappedValue property. Don’t forget to add the prefix @propertyWrapper before the property wrapper.

We will continue with some examples which explain the property wrappers. Let’s start by examining the examples 🤓

  • That property wrapper automatically capitalizes the value which is assigned to it.
  • Suppose we are tracking a data flow and we want to see the changes about property on Xcode console. Writing print(value) wherever the variable’s value changes would mess the code. In such cases, you should use property wrappers. The property wrapper below will be doing this for you. Isn’t it so cool? 😎

Let’s talk about property wrappers in SwiftUI 🤓

SwiftUI gives us some property wrappers, each of which provides different functionality. In this article, we will talk about commonly used them such as @State, @Binding, @ObservedObject, @EnvironmentObject, and @Environment.

@State

SwiftUI manages the storage of property that is declared as a state. If the state value changes, the view reruns the body to validate its appearance. A state property should only be accessible inside the view so state property should be declared as private.

@Binding

Using @Binding provides communication between a property that stores data and a view that displays and changes the data.

@ObservedObject

@ObservedObject property wrapper subscribes to an observable object and notifies the view if the observable object is changed. Any type that is marked with @ObservedObject must conforms ObservableObject protocol.

@EnvironmentObject

@EnvironmentObject property wrapper can be used for data that should be shared with various places in the application. It provides auto-update for the views when the data changes.

@Environment

@Environment property wrapper can be used to access and subscribe to changes of system-related values.

In this article, I tried to tell property wrappers and I touched briefly on commonly used them in SwiftUI. Thanks for reading, don’t forget to keep coding until the next story! 🤓

--

--