skiptools / skiptools/skip-ui

`@AppStorage` crashes instead of coercing types

Open
#317 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
330
Forks
76
Avg merge
3h 6m
Merged PRs (30d)
1

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
            }
        }
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue by opening the StringPreference screen, setting the value to "1.0", and then opening DoublePreference; inspect ContentView.kt around line 139 where the ClassCastException occurs. The fix is complete when the shared @AppStorage value can be read as both a String and a Double without crashing, matching the stated iOS behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, swift
Domain
mobile-dev
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.