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

huangapple go评论64阅读模式
英文:

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(&quot;Size of array is \(messageArray.count)&quot;) // 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(&quot;Press Me&quot;) {  // for testing
                calculate.addMessage(msg: &quot;Pressed&quot;)
            }  // 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)

huangapple
  • 本文由 发表于 2023年4月6日 22:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75950664.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定