Overview
- Below stated code doesn't work on macOS
- Selection is not retained across launches
My observation
- When I run on Xcode and stop running in Xcode, then the selection is retained when I run again.
- However if I run on Xcode and then Quit (Command Q) then the selection is not retained.
- The selection is not retained when the app is archived into a separate app.
Questions
- Am I missing something?
- What should I do to retain selection?
Code
struct ContentView: View {
@SceneStorage("selectedIndex") var selectedIndex: Int?
var body: some View {
List(0..<10, selection: $selectedIndex) { index in
Text("Cell \(index)")
.id(index)
}
}
}
CodePudding user response:
https://developer.apple.com/documentation/swiftui/scenestorage
If the Scene is explicitly destroyed (e.g. the switcher snapshot is destroyed on iPadOS or the window is closed on macOS), the data is also destroyed.
Given the above, SceneStorage
isn't a good fit for your requirement of persistent-across-launches.
AppStorage
may be a better choice, for example.