我有以下课程:
struct PriceFormatter {
@AppStorage(UserDefaultsKey.savedCurrency)
var savedCurrency: String?
let price: Float
init(price: Float) {
self.price = price
}
var formatted: String {
return "\(savedCurrency) \(price)"
}
}
Run Code Online (Sandbox Code Playgroud)
以及以下视图:
struct PriceText: View {
let price: Float
var body: some View {
Text(PriceFormatter(price: self.price).formatted)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望视图在更改后重新savedCurrency渲染UserDefaults。
我让它很容易工作,当它@AppStorage是视图的一部分时,但我不知道在这种情况下该怎么做。我尝试使用或尝试制作组合并订阅它,但@ObservableObject也没有成功。@PublishedPublisher