Initializer

init(_:store:)

Creates a property that can save and restore an Optional string, transforming it to an Optional RawRepresentable data type.

Declaration 宣言

init<R>(_ key: String, store: UserDefaults? = nil) where Value == Optional<R>, R : RawRepresentable, R.RawValue == String

Parameters パラメータ

key

The key to read and write the value to in the user defaults store. user defaults storeにおいてその値を読み出すそして書き出すためのキー。

store

The user defaults store to read and write to. A value of nil will use the user default store from the environment. それに読み出しおよび書き出しするuser defaults store。nilの値は、その環境からのuser default storeを使います。

Discussion 議論

Defaults to nil if there is no restored value

A common usage is with enumerations: 一般的な使用法は、列挙でとなります:


enum MyEnum: String {
    case a
    case b
    case c
}
struct MyView: View {
    @AppStorage("MyEnumValue") private var value: MyEnum?
    var body: some View { ... }
}

See Also 参照

Storing an Optional Value