List not showing info.

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

List not showing info

问题

以下是已翻译的内容:

"It may be one dumb and simple error, but it's been a while since I last touched Swift, so that I would appreciate the help.
I have one observable object (listOfWordsCounted) in the class ListToPrint, where I save the words I want to print.

The objective is to write a sentence in the textbox, count each word's number of occurrences, and display it. I chose a list cause it seemed appropriate, but I'm not obtaining any signal from the ForEach cycle."

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 {
                ForEach(self.listOfWordsCounted.wordsToPrint) { word in
                    Text(word.id)
                }
            }
        }
        .padding()
    }
}

I expected a list with the words I wrote in the textbox.

英文:

It may be one dumb and simple error, but it's been a while since I last touched Swift, so that I would appreciate the help.
I have one observable object (listOfWordsCounted) in the class ListToPrint, where I save the words I want to print.

The objective is to write a sentence in the textbox, count each word's number of occurrences, and display it. I chose a list cause it seemed appropriate, but I'm not obtaining any signal from the ForEach cycle.

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()
    }
}

I expected a list with the words I wrote in the textbox.

答案1

得分: 0

@Published var wordsToPrint: [Word] = []

英文:
@Published var wordsToPrint: [Word] = []

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

发表评论

匿名网友

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

确定