skip
skip copied to clipboard
Compile Error: using a function in @AppStorage() fails to compile if the enum type is inferred or if a parameter label is used
I found that when using a function as the @AppStorage key the function fails to infer the enum type causing an error (case 1, when typing 'Tab.tag(.welcome)'). By typing 'Tag.welcome' it fixes the error, although this is not needed outside the @AppStorage() wrapper. But if you use a function parameter label it also fails to compile (case 3, when typing 'Tab.tag(key: Tab.welcome').
Here's a simplified test:
struct ContentView: View {
....
// 1. Desired option. Doesn't work
@AppStorage(Tab.tag(.welcome)) var selectedTab1: Int = 0
// 2. Verbose option. It works
@AppStorage(Tab.tag(Tab.welcome)) var selectedTab2: Int = 0
// 3. Parameter labels don't work
@AppStorage(Tab.tag(key: Tab.welcome)) var selectedTab3: Int = 0
}
enum Tab : String, Hashable {
case welcome, home, settings
static func tag(_ key: Tab) -> String { key.rawValue }
static func tag(key: Tab) -> String { key.rawValue }
}