英文:
In SwiftUI I have a List that should update when the source array in a Swift class is appended but I get syntax errors that I don't understand
问题
这是您要翻译的内容的翻译版本:
我有一个示例项目,用于学习如何在源数组(Swift类中)更新时使列表更新。在SwiftUI和Swift方面,我还是新手。列表视图错误是:
初始化程序'init(_:id:rowContent:)'要求'Published<[String]>.Publisher'符合'RandomAccessCollection'
苹果表示,一个String数组确实符合'RandomAccessCollection'。
XCode版本14.3,MacOS Ventura 13.3
这是我第四次尝试使用我在Google上找到的建议,其中包括苹果的文档;我总是遇到我不理解的语法错误,但我的演示代码一直在变得更短。
我的目标是使用Calculate类来协调数据处理,并不时通知用户已经完成了什么,也就是运行日志。我希望从Calculate类及其子类中附加消息到数组中。
包含数组的类:
import Foundation
class Calculate: ObservableObject {
init() {
// 以后再做
}
@Published var messageArray = Array(arrayLiteral: String())
func addMessage(msg: String) {
messageArray.append(msg)
print("数组大小为 \(messageArray.count)") // 测试
}
}
我的内容视图如下。按钮视图仅用于验证代码是否有效。
import SwiftUI
import Combine
struct ContentView: View {
@ObservedObject var calculate = Calculate()
var body: some View {
VStack {
Button("按我") { // 用于测试
calculate.addMessage(msg: "已按")
} // 按钮动作确实附加到数组。
List(self.calculate.$messageArray, id: \.self) {
message in
Text(message)
}
}
.onReceive(self.calculate.$messageArray) {
newValue in // ??
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
英文:
I have an example project to learn how to have a List update when the source array (in a Swift class) updates. I'm pretty green in SwiftUI and Swift. The List View error is:
> Initializer 'init(_:id:rowContent:)' requires that
> 'Published<[String]>.Publisher' conform to 'RandomAccessCollection'
Apple says that an array of String does conform to a 'RandomAccessCollection'.
XCode ver. 14.3, MacOS Ventura 13.3
This is my fourth attempt to use suggestions that I've found using Dr. Google which includes Apple docummentation; I always get syntax errors that I don't understand but my demonstration code keeps getting shorter.
My objective is to use the Calculate class to orchestrate data processsing and, from time to time, inform the user of what's been done, aka a running log. I want to append messages to the array from the Calculate class and any of its sub-classes.
The class containing the array:
import Foundation
class Calculate: ObservableObject {
init() {
// TODO later
}
@Published var messageArray = Array(arrayLiteral: String())
func addMessage(msg: String) {
messageArray.append(msg)
print("Size of array is \(messageArray.count)") // test
}
}
My Content View is below. The Button View is only for verifying that the code works.
import SwiftUI
import Combine
struct ContentView: View {
@ObservedObject var calculate = Calculate()
var body: some View {
VStack {
Button("Press Me") { // for testing
calculate.addMessage(msg: "Pressed")
} // The button action does append to the array.
List(self.calculate.$messageArray, id: \.self) {
message in
Text(message)
}
}
.onReceive(self.calculate.$messageArray) {
newValue in // ??
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
答案1
得分: 1
List
初始化器期望一个数组,而不是数组的发布者,因此将 List
声明更改为:
List(self.calculate.messageArray, id: \.self)
英文:
The List
initializer expects an array and not publisher of an array so change the List
declaration to
List(self.calculate.messageArray, id: \.self)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论