-
-
Notifications
You must be signed in to change notification settings - Fork 40
Open
Description
On iOS, @AppStorage will coerce types, allowing you to set a value as a Double and read it back as a String or vice versa, like the sample below.
On Android, if you navigate to the StringPreference screen, set the preference to "1.0" and then navigate to the DoublePreference screen, the app will crash.
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
at showcase.module.DoublePreference.getPreference$Showcase_debug(ContentView.kt:139)
import SwiftUI
enum Destination: String, CaseIterable {
case stringPreference
case doublePreference
}
struct ContentView: View {
@State var navDestination: Destination = .stringPreference
var body: some View {
NavigationStack {
VStack {
NavigationLink(value: Destination.stringPreference) {
Text("String Preference")
}
NavigationLink(value: Destination.doublePreference) {
Text("Double Preference")
}
}
.navigationDestination(for: Destination.self) { destination in
switch destination {
case .stringPreference:
StringPreference()
case .doublePreference:
DoublePreference()
}
}
}
}
}
struct StringPreference: View {
@AppStorage("preference") var preference = "0.0"
var body: some View {
VStack {
Text("Preference: \(preference)")
Button("1.0") {
preference = "1.0"
}
Button("0.0") {
preference = "0.0"
}
}
}
}
struct DoublePreference: View {
@AppStorage("preference") var preference = 0.0
var body: some View {
VStack {
Text("Preference: \(preference)")
Button("1.0") {
preference = 1.0
}
Button("0.0") {
preference = 0.0
}
}
}
}Metadata
Metadata
Assignees
Labels
No labels