Hello everybody,
Im new to Swift programming or app programming in geenrell and now im working on my first project. However im struggling right now with something. I crated an object for my Quizzplayer. To give them a name I want to create a textfield for each one of them where the user can enter the name. let me give u a code snippet for my quizPlayer object:
class QuizPlayer: ObservableObject { @Published var name: String @Published var points: Int @Published var isActive: Bool
init(name: String) {
self.name = name
self.points = 0
self.isActive = false
}
func addPoints(pointsToAdd: Int) {
self.points += pointsToAdd
}
func changeName(newName: String) {
self.name = newName
}
}
I create 5 empty Quizzplayers at the beginning:
@State private var quizPlayers: [QuizPlayer] = Array(repeating: QuizPlayer(name: ""), count: 5)
then I create my Textfields and try to bind dem to the names of the players:
ForEach(0 ..< self.numberOfPlayers, id: .self) { index in TextField("Player (index + 1) name", text: self.$quizPlayers[index].name) .padding() }
But it's not working at all. very time I run the simulator and edit one textfield, every textfield gets changed so I can't edit them individually. I´m not sure if I used the State and ObservableObject right or what the issue is. Maybe someone understands my problem and can help me.
All the best
Leon