跳转至
Structure

AppStorage

A property wrapper type that reads from and writes to the device's storage.

Declaration

@frozen @propertyWrapper struct AppStorage<Value> : DynamicProperty

Overview

Use this property wrapper to read to and write from permanent storage on the user's device, also known as "UserDefaults".

Check out defaultAppStorage(_:) for Scenes and defaultAppStorage(_:) for Views to learn more about setting the default app storage location for a view.

Below shows a simple example using a String.

struct ContentView: View {
    @AppStorage("name") var name: String = "Kanye"

    var body: some View {
    Text("Stored string: \(name)")
          TextField(name, text: $name)
    }
}

Availability

iOS 14.0+

macOS 11.0+

tvOS 14.0+

watchOS 7.0+

Topics


Instance Property

projectedValue Provides a binding for the wrapped value.

wrappedValue The user default value for the given key.


Initializer

init(_:store:) Creates a property that can read and write an Optional double user default.

init(_:store:) Creates a property that can read and write an Optional boolean user default.

init(_:store:) Creates a property that can read and write an Optional URL user default.

init(_:store:) Creates a property that can read and write an Optional data user default.

init(_:store:) Creates a property that can read and write an Optional integer user default.

init(_:store:) Creates a property that can read and write an Optional string user default.

init(wrappedValue:_:store:) Creates a property that can read and write to an integer user default.

init(wrappedValue:_:store:) Creates a property that can read and write to a url user default.

init(wrappedValue:_:store:) Creates a property that can read and write to a string user default, transforming that to RawRepresentable data type.

init(wrappedValue:_:store:) Creates a property that can read and write to a boolean user default.

init(wrappedValue:_:store:) Creates a property that can read and write to an integer user default, transforming that to RawRepresentable data type.

init(wrappedValue:_:store:) Creates a property that can read and write to a double user default.

init(wrappedValue:_:store:) Creates a property that can read and write to a user default as data.

init(wrappedValue:_:store:) Creates a property that can read and write to a string user default.