【发布时间】:2021-10-05 12:37:02
【问题描述】:
我是 Swift 新手,想解决这个问题。我希望,当我关闭并返回应用程序时,选择器中的货币会被保存。 (通过@AppStorage)但是我不能使用我想到的任何东西在@AppStorage 中将$currency 定义为$cur。谢谢你的帮助!代码如下:
//Settings
struct SettingsView: View {
@State private var currency = "$"
var currencies = ["$", "€", "¥", "£"]
@AppStorage("name") var name = ""
@AppStorage("cur") var cur = "\($currency)"
//Cannot use instance member '$currency' within property initializer; property initializers run before 'self' is available
var body: some View {
NavigationView {
Form {
Section(header: Text("Your name")) {
TextField("Tim", text: $name)
}
Section(header: Text("Your currency")) {
Picker(selection: $currency, label: Text("Your Currency")) {
ForEach(currencies, id: \.self) {
Text($0)
}
}
.pickerStyle(SegmentedPickerStyle())
}
}
}
.navigationTitle("Settings")
}
}
【问题讨论】:
标签: swift xcode swiftui properties picker