I have a question regarding Swift where I just can't wrap my head around. I have a quite simple view. It's a list where you write down the name of each player and as soon as you hit return the name get's via onCommit
appended to the array playerArray
. So far this works fine.
Now I want to pass the names that are in this array to another view. How do I do that? Do I use @EnvironmentObject
, do I save this to @AppStorage
or do I need to use/save it to Core Data
and then fetch the data in the view where I need it?
In the next view I would like to show the name of player 1 first and when he finished the game then display the name of player 2 and when he finished the game then the name of player 3 and so on and so on. When everybody played the game I then need all the names again for the scoreboard. While writing this down I guess I also need a way to bound the score each player has achieved to the name.
Later on I would also like to have two teams with two or more players for each team. There the order would be team A, player 1; team B, player 1; team A, player 2; team B, player 2 and so on and so on.
Any hint on how to make this possible would be great. If I need to use Core Data to do that properly then I'll do a deep dive into Core Data.
Thank you!
https://i.stack.imgur.com/EgJ3p.gif
import SwiftUI
struct ContentView: View {
@State var playerField = ""
@State var playerArray: [String] = []
var body: some View {
VStack {
List {
ForEach(playerArray, id: \.self) { data in
Text(data)
}
TextField(
"Name",
text: $playerField,
onCommit: {
playerArray.append(playerField)
playerField = ""
}
)
} // List
} // VStack
} // some View
} // ContentView: View
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}