The @State SwiftUI Property Wrapper
Mutable state in SwiftUI
Although I’ve labeled this article as beginner, I’ve noticed that my previous article about property wrappers just didn’t make it seem so. So I think it really asks for a new article about the @State
property wrapper.
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
Terminology
Property Wrapper: A property wrapper adds a layer of separation between code that manages how a property is stored and the code that defines a property
@State: a property wrapper used to create a single source of truth for data in your app which mutates over time (and updating a view appropriately).
The @State property wrapper
In Swift, a struct is used to store variables of different data types and views are simple structs.
This leads us to a problem. This problem is that a view struct does not have a mutating function where you can change it’s properties. This means that @State
is a solution to this issue, as it can be used to manage mutable state in SwiftUI in the view
struct.