@Published 属性 没有触发任何东西
@Published property not triggering anything
我有这个协议:
protocol MyProtocol:ObservableObject {
var codeDetected:CodeDetected { get set }
var codeDetectedPublished: Published<CodeDetected> { get }
var codeDetectedPublisher: Published<CodeDetected>.Publisher { get }
}
这是 CodeDetected
class CodeDetected:ObservableObject {
var title: String
var value:String
init(title:String = "", value:String = ""){
self.title = title
self.value = value
}
}
稍后 class...
class MyModel: MyProtocol {
@Published var codeDetected:CodeDetected = CodeDetected()
var codeDetectedPublished: Published<CodeDetected> { _codeDetected}
var codeDetectedPublisher: Published<CodeDetected>.Publisher { $codeDetected }
}
问题是在 class 上更改 codeDetected
不会触发观察它的结构上的任何内容。
稍后实施
struct MyView: View {
@ObservedObject let myModel = MyModel()
我正在为它设置新的值,比如
codeDetected = newCodeDetected
尝试观察你的模型
@StateObject var myModel = MyModel()
没有 Minimal Reproducible Example 我们将在这里分崩离析。
您也没有观察到 title
和 value
。在他们身上放一个 @Published
然后观察他们自己的 View
protocol MyProtocol:ObservableObject {
var codeDetected:CodeDetected { get set }
//var codeDetectedPublished: Published<CodeDetected> { get }
//var codeDetectedPublisher: Published<CodeDetected>.Publisher { get }
}
class CodeDetected:ObservableObject {
@Published var title: String
@Published var value:String
init(title:String = "init", value:String = "init"){
self.title = title
self.value = value
}
}
class MyModel: MyProtocol {
@Published var codeDetected:CodeDetected = CodeDetected()
//var codeDetectedPublished: Published<CodeDetected> { _codeDetected}
//var codeDetectedPublisher: Published<CodeDetected>.Publisher { $codeDetected }
}
struct CodeDetectedView: View {
@StateObject var myModel = MyModel()
var body: some View {
VStack{
//Can't see updates here unless you ask for them/tell the parent model there are changes
Text(myModel.codeDetected.title)
.onReceive(myModel.codeDetected.$title, perform: { _ in
myModel.objectWillChange.send()
})
Text(myModel.codeDetected.value)
.onReceive(myModel.codeDetected.$value, perform: { _ in
myModel.objectWillChange.send()
})
CodeDetectedSubView(codeDetected: myModel.codeDetected)
}
}
}
struct CodeDetectedSubView: View {
@ObservedObject var codeDetected: CodeDetected
var body: some View {
//Can see here
Text(codeDetected.title)
Text(codeDetected.value)
TextField("title", text: $codeDetected.title)
TextField("value", text: $codeDetected.value)
}
}
我有这个协议:
protocol MyProtocol:ObservableObject {
var codeDetected:CodeDetected { get set }
var codeDetectedPublished: Published<CodeDetected> { get }
var codeDetectedPublisher: Published<CodeDetected>.Publisher { get }
}
这是 CodeDetected
class CodeDetected:ObservableObject {
var title: String
var value:String
init(title:String = "", value:String = ""){
self.title = title
self.value = value
}
}
稍后 class...
class MyModel: MyProtocol {
@Published var codeDetected:CodeDetected = CodeDetected()
var codeDetectedPublished: Published<CodeDetected> { _codeDetected}
var codeDetectedPublisher: Published<CodeDetected>.Publisher { $codeDetected }
}
问题是在 class 上更改 codeDetected
不会触发观察它的结构上的任何内容。
稍后实施
struct MyView: View {
@ObservedObject let myModel = MyModel()
我正在为它设置新的值,比如
codeDetected = newCodeDetected
尝试观察你的模型
@StateObject var myModel = MyModel()
没有 Minimal Reproducible Example 我们将在这里分崩离析。
您也没有观察到 title
和 value
。在他们身上放一个 @Published
然后观察他们自己的 View
protocol MyProtocol:ObservableObject {
var codeDetected:CodeDetected { get set }
//var codeDetectedPublished: Published<CodeDetected> { get }
//var codeDetectedPublisher: Published<CodeDetected>.Publisher { get }
}
class CodeDetected:ObservableObject {
@Published var title: String
@Published var value:String
init(title:String = "init", value:String = "init"){
self.title = title
self.value = value
}
}
class MyModel: MyProtocol {
@Published var codeDetected:CodeDetected = CodeDetected()
//var codeDetectedPublished: Published<CodeDetected> { _codeDetected}
//var codeDetectedPublisher: Published<CodeDetected>.Publisher { $codeDetected }
}
struct CodeDetectedView: View {
@StateObject var myModel = MyModel()
var body: some View {
VStack{
//Can't see updates here unless you ask for them/tell the parent model there are changes
Text(myModel.codeDetected.title)
.onReceive(myModel.codeDetected.$title, perform: { _ in
myModel.objectWillChange.send()
})
Text(myModel.codeDetected.value)
.onReceive(myModel.codeDetected.$value, perform: { _ in
myModel.objectWillChange.send()
})
CodeDetectedSubView(codeDetected: myModel.codeDetected)
}
}
}
struct CodeDetectedSubView: View {
@ObservedObject var codeDetected: CodeDetected
var body: some View {
//Can see here
Text(codeDetected.title)
Text(codeDetected.value)
TextField("title", text: $codeDetected.title)
TextField("value", text: $codeDetected.value)
}
}