我收到错误
无法分配给属性:“ viewModel”是仅获取属性 尝试使用泛型类型的@EnvironmentObject时。
import SwiftUI
import Combine
protocol HasCount {
var count: Int { get set }
var countPublished: Published<Int> { get set }
var countPublisher: Published<Int>.Publisher { get set }
}
struct SomeView<ViewModelType: ObservableObject & HasCount>: View {
@EnvironmentObject var viewModel: ViewModelType
var body: some View {
return VStack {
Text("text")
.onAppear {
self.viewModel.count = 1
}
}
}
}
有人知道如何解决这个问题吗?
答案 0 :(得分:0)
您的HasCount
协议只限于以下对象
protocol HasCount: AnyObject { // << here !
var count: Int { get set }
var countPublished: Published<Int> { get set }
var countPublisher: Published<Int>.Publisher { get set }
}