Property Wrapper
Tags: swift, ios, mobile, development
Published: 2023-05-08 12:37:59

If you’ve ever developed iOS apps, you’ve probably heard about properties in Swift, one of which is called Property Wrappers. But what are they exactly?

Imagine that you want to have some level of control over a property in your application, and you want to ensure that it always has a default value or that the assigned value falls within a certain range. you can use a Property Wrapper.

Property Wrappers are a new feature introduced in Swift 5.1 that allow adding custom behavior to properties. It’s like a package that you can apply to multiple properties, so you don’t have to repeat the same behavior over and over again. You can encapsulate the behavior in a reusable type.

A Property Wrapper can be defined for any type of property, whether it’s a property of a class, struct, enum, or a custom type. When a Property Wrapper is applied to a property, it defines the logic that will be used to set and retrieve the property value.

This can be defined using the @propertyWrapper annotation. When applying the annotation to a property, the compiler transforms the property definition so that it uses the Property Wrapper instead of the original type.

## **Applying the property wrapper**


The username property is defined as a UserDefaultsWrapper Property Wrapper, allowing to store and access the value from UserDefaults without needing to use the standard get/set methods. The default value for the key “username” is “Guest”.

In the viewDidLoad method, we are printing the current value of the username property, which should be "Guest" since we have not yet changed the value in UserDefaults. Then, we are changing the stored value in UserDefaults through the username property and printing it again to verify if the update was successful.

## **SwiftUI Property Wrappers**

Let’s list some SwiftUI Property Wrappers, which are being heavily used for development. Are they:

@State: A property wrapper used to declare a value that can be modified and triggers a view update when the value changes. @State is typically used for view-specific data that’s not needed outside of the view itself.
@Published: A property wrapper used in Swift’s Combine framework to declare a publisher that emits a change event when the wrapped value changes. @Published is commonly used to publish changes to model objects in a SwiftUI app.
@ObservedObject: A property wrapper used to declare a reference to an external object that conforms to the ObservableObject protocol. The observed object is responsible for publishing changes to its data, and any views that depend on that data are updated automatically.
@EnvironmentObject: A property wrapper used to declare a reference to an object that’s managed by the SwiftUI environment. The object is passed down the view hierarchy from a parent view, and any child view can access it using @EnvironmentObject.
@Binding: A property wrapper used to declare a reference to a mutable value that’s owned by another view or view controller. The value is passed down to a child view using a binding, which allows the child view to modify the value and propagate the changes back up the view hierarchy.

## **@State vs @Published**

State and @Published are both property wrappers used in SwiftUI to allow views to be automatically updated when an underlying property is modified. However, there are some important differences between the two.

@State is used to store the state of a view that can be modified during the view’s lifecycle. It is declared as a property in a view and can be updated internally by the view itself. When a @State value is updated, the view is automatically redrawn and the new version is displayed on the screen. This means that @State is useful for storing values that can dynamically change, such as whether a button has been pressed or if a form has been filled out.

On the other hand, @Published is used to allow an external object to modify a property of a view. It is declared as a property in an ObservableObject class. When a @Published value is updated, it notifies all the views that are observing it, and then the views are automatically updated. This makes @Published useful when you want a property of a view to be updated by an external object, such as a data model.

In summary, @State is used to store values that are internally modified by a view, while @Published is used to allow external objects to modify properties of a view and notify the changes to the views.

## **Conclusion**

Property wrappers are one of the insane features in Swift 5.1 as they allow us to customize and reuse our code in new ways. With property wrappers, we can implement functionality directly at the property level, which opens up many possibilities.
While property wrappers are useful, they can also be a little tricky. On the one hand, they allow us to access and assign encapsulated properties in the same way as non-encapsulated ones. But on the other hand, the risk is that we can end up hiding a lot of functionality behind a non-obvious abstraction.
And you? Have you started using property wrappers yet, or do you have a use case in mind?
Share your experiences and ideas with us via [Twitter](https://twitter.com/swiftbydrew), [Dev.to](https://dev.to/swiftbydrew) or [email](mailto:andrewdev.us@icloud.com).
Thanks for reading! 🚀