How do I store state in modern SwiftUI apps?

Generated on 7/30/2024

1 search

Tailscan

This post is brought to you by Tailscan, the best devtool for Tailwind CSS.

To store state in modern SwiftUI apps, you can use several tools provided by SwiftUI, such as @State and @Binding. Here are the key points:

  1. State:

    • @State is used to create a new internal source of data for a view. SwiftUI manages the storage and provides it back for the view to read and write.
    • Example: If you have a view that allows you to rate your pet's tricks, you can use @State to keep track of the current rating and change it over time. SwiftUI maintains the value of this state behind the scenes and updates the UI when the state changes.
    • SwiftUI essentials (08:41)
  2. Binding:

    • @Binding creates a two-way reference to the state of some other view. This is useful when you need to pass state down to child views and allow them to modify it.
    • Example: If you have a parent view that holds the state and a child view that needs to modify this state, you can pass a binding to the child view.
  3. ObservableObject and @Published:

    • For more complex state management, you can use ObservableObject and @Published properties. An ObservableObject is a class that conforms to the ObservableObject protocol, and @Published properties within this class will automatically notify any observing views when they change.
    • Example: You can create an observable pet class that SwiftUI will track for changes. When a property in this class changes, SwiftUI will update any views that depend on this property.
    • SwiftUI essentials (09:43)

These tools help you manage state effectively in SwiftUI, ensuring that your UI stays in sync with your data.

Relevant Sessions

SwiftUI essentials

SwiftUI essentials

Join us on a tour of SwiftUI, Apple’s declarative user interface framework. Learn essential concepts for building apps in SwiftUI, like views, state variables, and layout. Discover the breadth of APIs for building fully featured experiences and crafting unique custom components. Whether you’re brand new to SwiftUI or an experienced developer, you’ll learn how to take advantage of what SwiftUI has to offer when building great apps.

Track model changes with SwiftData history

Track model changes with SwiftData history

Reveal the history of your model’s changes with SwiftData! Use the history API to understand when data store changes occurred, and learn how to use this information to build features like remote server sync and out-of-process change handing in your app. We’ll also cover how you can build support for the history API into a custom data store.

Create a custom data store with SwiftData

Create a custom data store with SwiftData

Combine the power of SwiftData’s expressive, declarative modeling API with your own persistence backend. Learn how to build a custom data store and explore how to progressively add persistence features in your app. To get the most out of this session, watch “Meet SwiftData” and “Model your schema with SwiftData” from WWDC23.

Enhance your UI animations and transitions

Enhance your UI animations and transitions

Explore how to adopt the zoom transition in navigation and presentations to increase the sense of continuity in your app, and learn how to animate UIKit views with SwiftUI animations to make it easier to build animations that feel continuous.