<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/AppStorage/init(_:store:)-9lku2",
  "metadataVersion" : "0.1.0",
  "role" : "Initializer",
  "symbol" : {
    "kind" : "Initializer",
    "modules" : [
      "SwiftUI",
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI10AppStorageV01_a5Data_aB0E_5storeACy0aE020PersistentIdentifierVSgGSS_So14NSUserDefaultsCSgtcAIRszlufc"
  },
  "title" : "init(_:store:)"
}
-->

# init(_:store:)

Creates a property that can read and write an Optional data user
default via `PersistentIdentifier`.

```
init(_ key: String, store: UserDefaults? = nil) where Value == PersistentIdentifier?
```

## 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.

```
@Model class Recipe { ... }
struct RecipeList: View {
    @Query var recipes: [Recipe]
    @SceneStorage("selectedID") var selectedRecipeID: Recipe.ID?

    var body: some View {
        List(recipes, selection: $selectedRecipeID) { recipe in
            RecipeDetail(recipe)
        }
    }
}
```

---

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)