英文:
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] = []
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论