我想在 SwiftUI 中有一个可选的@ObservedObject,但我不断收到编译时错误。
Property type 'AModel?' does not match that of the 'wrappedValue' property of its wrapper type 'ObservedObject'
这是一些最小的可重现代码。
import SwiftUI
public struct AView: View {
//MARK: View Model
//Error thrown here.
@ObservedObject var model: AModel?
//MARK: Body
public var body: some View {
Text("\(model?.value ?? 0)")
}
//MARK: Init
public init() {
}
}
class AModel: ObservableObject {
let value: Int = 0
}