ram*_*nok 7 swift swiftui combine
我有以下课程:
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
正如 @Klaas 所指出的,从 iOS 14.5 开始,可以@AppStorage在ObservableObject( https://developer.apple.com/documentation/ios-ipados-release-notes/ios-ipados-14_5-release-notes )中使用
工作解决方案现在看起来像这样:
class PriceFormatter: ObservableObject {
@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 {
@StateObject var formatter = PriceFormatter(price: 5)
var body: some View {
Text(formatter.formatted)
}
}
Run Code Online (Sandbox Code Playgroud)
正如 @Asperi 所指出的,@AppStorage应该仅在View. 隐含地它也写在这里: https: //developer.apple.com/documentation/swiftui/appstorage
| 归档时间: |
|
| 查看次数: |
5044 次 |
| 最近记录: |