Swift Combine and SwiftUI 理解需要更正
Swift Combine and SwiftUI understanding needs corrections
我尝试做一些简单的事情。在 AppDelegate 中我需要 var vertices: [SCNVector3] = []
。在 @IBAction func getVertices(_ sender: Any)
中,我可以读取文件并将新值分配给 vertices
。简单有效。但是当我尝试将值传递给 SwiftUI View 时,我遇到了问题。
如果我定义
@State var vertices: [SCNVector3] = []
func applicationDidFinishLaunching(_ aNotification: Notification) {
....
let contentView = CloudView(data: $vertices)
....
window.contentView = NSHostingView(rootView: contentView)
...
}
@IBAction func getVertices(_ sender: Any) {
...
do {
let readVertices: [SCNVector3] = try... // read file and convert to [SCNVector3]
vertcices = readVertices // assign or not to assign, this is a question
print (readVertices.count, vertices.count)
}
...
}
并打印:
3500 0
因此,它永远不会更新 CloudView
,vertices
始终是一个空数组。
有人可以向我解释一下我应该如何以正确的方式做到这一点吗?
您不能在 SwiftUI 视图上下文之外使用 @State
。在这种情况下最合适的是使用 ObservableObject
,例如
class VerticesStorage: ObservableObject {
@Published var vertices: [SCNVector3] = []
}
然后在 AppDelegate
let verticesStorage = VerticesStorage() // initialize property
func applicationDidFinishLaunching(_ aNotification: Notification) {
....
let contentView = CloudView(data: verticesStorage) // inject reference
....
window.contentView = NSHostingView(rootView: contentView)
...
}
@IBAction func getVertices(_ sender: Any) {
...
do {
let readVertices: [SCNVector3] = try... // read file and convert to [SCNVector3]
verticesStorage.vertcices = readVertices // update here !!
print (readVertices.count, vertices.count)
}
...
}
现在在 SwiftUI 部分
struct CloudView: View {
@ObservedObject var data: VerticesStorage // observable !!
var body: some View {
// present here
}
}
我尝试做一些简单的事情。在 AppDelegate 中我需要 var vertices: [SCNVector3] = []
。在 @IBAction func getVertices(_ sender: Any)
中,我可以读取文件并将新值分配给 vertices
。简单有效。但是当我尝试将值传递给 SwiftUI View 时,我遇到了问题。
如果我定义
@State var vertices: [SCNVector3] = []
func applicationDidFinishLaunching(_ aNotification: Notification) {
....
let contentView = CloudView(data: $vertices)
....
window.contentView = NSHostingView(rootView: contentView)
...
}
@IBAction func getVertices(_ sender: Any) {
...
do {
let readVertices: [SCNVector3] = try... // read file and convert to [SCNVector3]
vertcices = readVertices // assign or not to assign, this is a question
print (readVertices.count, vertices.count)
}
...
}
并打印:
3500 0
因此,它永远不会更新 CloudView
,vertices
始终是一个空数组。
有人可以向我解释一下我应该如何以正确的方式做到这一点吗?
您不能在 SwiftUI 视图上下文之外使用 @State
。在这种情况下最合适的是使用 ObservableObject
,例如
class VerticesStorage: ObservableObject {
@Published var vertices: [SCNVector3] = []
}
然后在 AppDelegate
let verticesStorage = VerticesStorage() // initialize property
func applicationDidFinishLaunching(_ aNotification: Notification) {
....
let contentView = CloudView(data: verticesStorage) // inject reference
....
window.contentView = NSHostingView(rootView: contentView)
...
}
@IBAction func getVertices(_ sender: Any) {
...
do {
let readVertices: [SCNVector3] = try... // read file and convert to [SCNVector3]
verticesStorage.vertcices = readVertices // update here !!
print (readVertices.count, vertices.count)
}
...
}
现在在 SwiftUI 部分
struct CloudView: View {
@ObservedObject var data: VerticesStorage // observable !!
var body: some View {
// present here
}
}