这可能是一个愚蠢而简单的错误,但我已经有一段时间没有接触Swift 了,所以我会感谢你的帮助. 我有一个属于ListToPrint类的观察对象(ListOfWordsCounted),我将在其中保存要打印的单词.

目标是在文本框中写下一个句子,然后统计每个单词的出现次数,最后显示它.我 Select 了一个列表,因为它看起来很合适,但我没有从ForEach循环中获得任何信号.

import SwiftUI
import Foundation

class Word :Identifiable {
    var id:String
    var nTimes:Int = 1
    
    init(string: String) {
        self.id = string
    }
    
    func count(){self.nTimes += 1}
}

class ListToPrint : ObservableObject {
    var wordsToPrint:[Word] = []
    
    init(){}
    
    init(sentence :String){
        self.update(sentence: sentence)
    }
    
    func update(sentence :String){
        let componentsOfSentence = getStrComponentsArray(str: sentence)
        
        for comp in componentsOfSentence {
            
            if wordsToPrint.first(where: {$0.id == comp}) != nil
            {
                wordsToPrint.first(where: {$0.id == comp})!.count()
            } else
            {
                wordsToPrint.append(Word(string: comp))
            }
        }
        
        
    }
        
    
    func getStrComponentsArray(str :String) -> [String] {
        return str.components(separatedBy: " ")
    }
    
}

struct ContentView: View {
    @State private var sentence = ""
    @ObservedObject private var listOfWordsCounted = ListToPrint()
        
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
            
            
            TextField("Let me count your sentence", text: $sentence)
            Button("Count")
            {
                listOfWordsCounted.update(sentence: sentence)
                print(listOfWordsCounted.wordsToPrint)
                
            }
            .foregroundColor(Color.red)
            .padding()
            .background(Color(red: 0, green: 0, blue: 0.5))
            .clipShape(Capsule())

            Button("Print")
            {
                for word in listOfWordsCounted.wordsToPrint {
                    print(word.id)
                }
            }

            
            List {
                //                Text(" appeared " + "times")
                
                ForEach(self.listOfWordsCounted.wordsToPrint){ word in
                    Text(word.id)
                }
            }
            
        }
        .padding()
    }
}

我期待着出现一个列表,其中包含我在文本框中写下的单词.

推荐答案

@Published var wordsToPrint: [Word] = []

Swift相关问答推荐

启用完成并发判断以及如何解决警告(续)

如何在visionOS上删除PhotosPicker背景 colored颜色 ?

它是RxSwift中直接用于可扩展视图的有效的可扩展BehaviorRelay值?

可选,类似于自定义类型的初始化

如何在macOS中延迟退出应用程序的退出操作?

在扩展中创建通用排序函数

为什么Swift可以在没有足够信息的情况下推断类型?

使用View()和List()无法显示影子

Swift Property Wrappers-Initialization 失败并显示无法将‘Double’类型的值转换为预期的参数类型

在领域 SwiftUI 异常中使用 Apple 登录:无法识别的 Select 器发送到实例

如何在 swiftui 中设置给定字符串类型日期的日期倒计时?

使用 Swiftui 水平修剪 Shape()

不要从 NumberFormatter 获取货币符号

理解 Swift 2.2 Select 器语法 - #selector()

在 Swift 中强制崩溃的最简单方法

EXC_BREAKPOINT 崩溃的原因范围

使 struct 可散列?

如何更改弹出框的大小

如何在 Swift 中遍历 struct 属性?

使用 DispatchTime.now() + float 延迟?