What is @state

Asked on 09/15/2024

1 search

The concept of "state" is discussed in several sessions at WWDC 2024, particularly in the context of machine learning models and SwiftUI.

  1. Machine Learning Models: In the session "Bring your machine learning and AI models to Apple silicon," state is described as a persistent element that can be used across different runs of a model. This is particularly useful for stateful models, where the state can store information across different inference runs, such as in an accumulator example that keeps track of the summation of all historical inputs. Core ML now supports stateful models, allowing the model to automatically update state tensors without needing to define them as inputs or outputs. This leads to better performance and efficiency (Bring your machine learning and AI models to Apple silicon).

  2. SwiftUI: In the session "SwiftUI essentials," state is used to manage the internal data of a view. When a view property is marked as state, SwiftUI manages the storage and provides it back for the view to read and write. This allows for dynamic updates to the UI based on changes in the state, such as incrementing a value when a button is tapped (SwiftUI essentials).

These sessions highlight the importance of state in both machine learning and UI development, providing tools and frameworks to manage state efficiently.