Hi all, I have a picker populated by an enum... works just fine. Problem is when I try to localize my app, I get a compiler error in my picker that says "Instance method 'tag' requires that 'LocalizedStringKey' conform to 'Hashable'". I don't understand what this means. Here is my code:
localizable string file (excerpt)
//eng
"additionOnly" = "Addition Only"; "subtractionOnly" = "Subtraction Only"; "both" = "Both Addition and Subtraction";
// in VM:
enum MathMode: String, Hashable, CaseIterable {
case additionOnly = "additionOnly"
case subtractionOnly = "subtractionOnly"
case bothAdditionAndSubtraction = "both"
var localizedName: LocalizedStringKey { LocalizedStringKey(rawValue) }
}
@Published var selectedMode: String = UserDefaults.standard.string(forKey: "mathMode") ?? "Addition Only" {
didSet {
UserDefaults.standard.set(self.selectedMode, forKey: "mathMode")
print("portrait mode did set: \(selectedMode)")
}
}
//in View:
Menu {
Picker(selection: $settingsViewViewModel.selectedMode) {
ForEach(SettingsViewViewModel.MathMode.allCases, id: \.self) { mathMode in
Text(mathMode.localizedName)
.tag(mathMode.localizedName) //***
}
} label: {}
} label: {
Text(settingsViewViewModel.selectedMode)
.font(.title2)
}
.id(settingsViewViewModel.selectedMode)
*** this is the problem. if I use mathMode.rawValue... the code compiles, but in the picker I'm seeing "additionOnly" as my choice, rather than the value from the string file. If there's a better way to write all this, great, happy to change. Also, if I do away with the .tag altogether it still doesn't work. Thanks for your help.