<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/AppStorage/init(_:store:)-6aj8x",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI10AppStorageV_5storeACyqd__SgGSS_So14NSUserDefaultsCSgtcAERszSYRd__SS8RawValueRtd__lufc"
  },
  "title" : "init(_:store:)"
}
-->

# init(_:store:)

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

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

## Parameters

`key`

The key to read and write the value to in the 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.

## 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 { ... }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)