无法通过 ForEach 按钮更新 @State 变量(Int Counter)?
Update @State variable (Int Counter) through ForEach button not possible?
尝试通过遍历 ForEach 按钮来更新计数器,但在 Xcode11 中收到以下错误:
Cannot convert value of type >'ForEach, >_ModifiedContent)>>, >PaddingLayout>>' to closure result type ''
已尝试添加@State 但仍无法更新 var characterList 中的计数
import SwiftUI
struct CharacterSelection:Identifiable {
var id: Int
var name : String
var count: Int
}
import SwiftUI
struct ContentView : View {
@State var charactersList = [
CharacterSelection(id: 0, name: "Witch", count: 0),
CharacterSelection(id: 1, name: "Seer", count: 1),
CharacterSelection(id: 2, name: "Hunter", count: 0),
CharacterSelection(id: 3, name: "Knight", count: 0)
]
var body: some View {
VStack(alignment:.leading) {
ForEach(charactersList.identified(by: \.id)) {character in
HStack{
Text(character.name)
Spacer()
Text("\(character.count)
Button(action: { character.count += 1 }) {
Text("Button")
}
}.padding(10)
}
}
}
}
点击按钮时,var CharacterList 中相应索引的计数应该 += 1。
请注意,Arrays 和 CharacterSelection 都是 Value 类型,而不是 Reference 类型。如果您不知道其中的区别,请查看此页面:https://developer.apple.com/swift/blog/?id=10
为了使您的代码正常工作,您可以这样重写它:
struct ContentView : View {
@State var charactersList = [
CharacterSelection(id: 0, name: "Witch", count: 0),
CharacterSelection(id: 1, name: "Seer", count: 1),
CharacterSelection(id: 2, name: "Hunter", count: 0),
CharacterSelection(id: 3, name: "Knight", count: 0)
]
var body: some View {
VStack(alignment:.leading) {
ForEach(0..<charactersList.count) { i in
HStack{
Text(self.charactersList[i].name)
Spacer()
Text("\(self.charactersList[i].count)")
Button(action: { self.charactersList[i].count += 1 }) {
Text("Button")
}
}.padding(10)
}
Button(action: {
self.charactersList.append(CharacterSelection(id: self.charactersList.count, name: "something", count: 0))
}, label: {
Text("Add CharacterSelection")
})
}
}
}
尝试通过遍历 ForEach 按钮来更新计数器,但在 Xcode11 中收到以下错误:
Cannot convert value of type >'ForEach, >_ModifiedContent)>>, >PaddingLayout>>' to closure result type ''
已尝试添加@State 但仍无法更新 var characterList 中的计数
import SwiftUI
struct CharacterSelection:Identifiable {
var id: Int
var name : String
var count: Int
}
import SwiftUI
struct ContentView : View {
@State var charactersList = [
CharacterSelection(id: 0, name: "Witch", count: 0),
CharacterSelection(id: 1, name: "Seer", count: 1),
CharacterSelection(id: 2, name: "Hunter", count: 0),
CharacterSelection(id: 3, name: "Knight", count: 0)
]
var body: some View {
VStack(alignment:.leading) {
ForEach(charactersList.identified(by: \.id)) {character in
HStack{
Text(character.name)
Spacer()
Text("\(character.count)
Button(action: { character.count += 1 }) {
Text("Button")
}
}.padding(10)
}
}
}
}
点击按钮时,var CharacterList 中相应索引的计数应该 += 1。
请注意,Arrays 和 CharacterSelection 都是 Value 类型,而不是 Reference 类型。如果您不知道其中的区别,请查看此页面:https://developer.apple.com/swift/blog/?id=10
为了使您的代码正常工作,您可以这样重写它:
struct ContentView : View {
@State var charactersList = [
CharacterSelection(id: 0, name: "Witch", count: 0),
CharacterSelection(id: 1, name: "Seer", count: 1),
CharacterSelection(id: 2, name: "Hunter", count: 0),
CharacterSelection(id: 3, name: "Knight", count: 0)
]
var body: some View {
VStack(alignment:.leading) {
ForEach(0..<charactersList.count) { i in
HStack{
Text(self.charactersList[i].name)
Spacer()
Text("\(self.charactersList[i].count)")
Button(action: { self.charactersList[i].count += 1 }) {
Text("Button")
}
}.padding(10)
}
Button(action: {
self.charactersList.append(CharacterSelection(id: self.charactersList.count, name: "something", count: 0))
}, label: {
Text("Add CharacterSelection")
})
}
}
}