Decide which property wrapper is the right choice for your needs.
SwiftUI uses property wrappers to understand how we create and store data in our views, but if you need helping choosing which property wrapper is right for you I've made a tool to help. To find the right property wrapper for you, answer the questions below 🚀
1. Are you storing a struct, an enum, or a class?
2. Will the value change over time? 3. Are you creating the value in your view, or will it be passed in from somewhere else? 4. Is the property being used to track some sort of user input? You should use the Although you could also use You can learn more about the You should use the You can learn more about the You should use the Technically there's nothing stopping you from using You can learn more about the You should use the Using You can learn more about the You don't need a property wrapper here, and can use a regular property instead. For example: You can learn more about properties here: Properties in Swift. 2. Is it an object you're creating locally in your view, or will it be created somewhere else and passed in to your view? You should use the Using You can learn more about the 3. Will the object be in the SwiftUI environment, or do you want to pass it in manually? You should use the Important: if you fail to provide the object in the environment your app will crash. You can learn more about the You should use the Using You can learn more about the I want to store a struct or an enum.
Yes, it will change as my app runs.
I'll be creating the value locally in my view.
Yes, this is will track an active gesture.
@GestureState
property wrapper, like this:@GestureState private var dragAmount = CGSize.zero
@State
for this purpose, @GestureState
is significantly more efficient.@GestureState
property wrapper here: What is the @GestureState property wrapper?Yes, this will track which view has keyboard focus.
@FocusState
property wrapper, like this:@FocusState private var isUsernameFocused: Bool
@FocusState
property wrapper here: What is the @FocusState property wrapper?No, this is just a regular value.
@State
property wrapper, like this:@State private var username = "Taylor"
@State
to store a class instance, but you wouldn't get notified of any changes so it's best to avoid this.@State
property wrapper here: What is the @State property wrapper?I'll be passing in the value from somewhere else.
@Binding
property wrapper.@Binding var username: String
@Binding
means you'll need to pass the value into the view from somewhere else, but any changes you make to the value will be reflected in the original value too.@Binding
property wrapper here: What is the @Binding property wrapper?No, it won't change.
let username: String
I want to store a class.
I'm creating the object locally in my view.
@StateObject
property wrapper, like this:@StateObject private var user = User()
@StateObject
rather than @ObservedObject
is important, because it tells SwiftUI your view owns the object. This will stop SwiftUI from accidentally destroying the object if your view is recreated.@StateObject
property wrapper here: What is the @StateObject property wrapper?The object will be passed in from somewhere else.
The object will be in the SwiftUI environment.
@EnvironmentObject
property wrapper, like this:@EnvironmentObject var user: User
@EnvironmentObject
property wrapper here: What is the @EnvironmentObject property wrapper?I want to pass it in manually as a parameter.
@ObservedObject
property wrapper, like this:@ObservedObject var user: User
@ObservedObject
rather than @StateObject
is important, because it tells SwiftUI your view wants to be notified of changes without owning the object.@ObservedObject
property wrapper here: What is the @ObservedObject property wrapper?
There are other property wrappers not included in this tool because they are designed for specific purposes, including:
UserDefaults
.If you'd like more information on how property wrappers work in SwiftUI, I have some articles that will help:
You might also want to read SwiftUI Property Wrappers from Donny Wals.
Link copied to your pasteboard.