跳转至
Structure

SceneStorage

A property wrapper type that reads and writes to persisted, per-scene storage.

Declaration

@frozen @propertyWrapper struct SceneStorage<Value> : DynamicProperty

Overview

You use SceneStorage when you need automatic state restoration of the value. SceneStorage works very similar to State, except its initial value is restored by the system if it was previously saved, and the value is· shared with other SceneStorage variables in the same scene.

The system manages the saving and restoring of SceneStorage on your behalf. The underlying data that backs SceneStorage is not available to you, so you must access it via the @SceneStorage property wrapper. The system makes no guarantees as to when and how often the data will be persisted.

Each Scene has its own notion of SceneStorage, so data is not shared between scenes.

Ensure that the data you use with SceneStorage is lightweight. Data of a large size, such as model data, should not be stored in SceneStorage, as poor performance may result.

If the Scene is explictly destroyed (e.g. the switcher snapshot is destroyed on iPadOS or the window is closed on macOS), the data is also destroyed. Do not use SceneStorage with sensitive data.

If you would like your data to be stored permanently in the device, use AppStorage instead.

There are 6 possible scene storage types, and 6 corresponding initializers:

  1. Bool
  2. Int
  3. Double
  4. String
  5. URL
  6. Data

Here is a simple example using the common property wrapper syntax:

struct ContentView: View {
    @SceneStorage("name") private var name = "Kanye"

    var body: some View {
        TextField(name, text: $name)
    }
}
A screenshot displaying a keyboard view on a white background with a text field above it that reads "Kanye."

Availability

iOS 14.0+

macOS 11.0+

tvOS 14.0+

watchOS 7.0+

Topics


Instance Property

projectedValue A binding to the state value.

wrappedValue The underlying value referenced by the state variable.


Initializer

init(_:) Creates a property that can save and restore an Optional data.

init(_:) Creates a property that can save and restore an Optional integer.

init(_:) Creates a property that can save and restore an Optional boolean.

init(_:) Creates a property that can save and restore an Optional string.

init(_:) Creates a property that can save and restore an Optional URL.

init(_:) Creates a property that can save and restore an Optional double.

init(wrappedValue:_:) Creates a property that can save and restore a string, transforming it to a RawRepresentable data type.

init(wrappedValue:_:) Creates a property that can save and restore a string.

init(wrappedValue:_:) Creates a property that can save and restore a URL.

init(wrappedValue:_:) Creates a property that can save and restore a double.

init(wrappedValue:_:) Creates a property that can save and restore an integer, transforming it to a RawRepresentable data type.

init(wrappedValue:_:) Creates a property that can save and restore an integer.

init(wrappedValue:_:) Creates a property that can save and restore a boolean.

init(wrappedValue:_:) Creates a property that can save and restore data.