I am trying to create a list in my app which acts as a leaderboard. It should add a new value when a button on the other main view is tapped. However, I keep getting this error and I am not sure why. Here is the code for the view of the list:
'''
struct scoreView: View {
@Binding var scoreTracker: Int
@Binding var gameTracker: Int
@Binding var gameScores: [Any]
@State var counter = 0
var body: some View {
Text("Scores: ")
VStack {
List {
ForEach(1..<gameTracker) {
Text("Game \($0): \(gameScores[gameTracker]) ")
}
}
}
}
}
''' I created an array on my contentView to append a value to it after a button is clicked a certain number of times. However, when I try interpolating that value in the text of the list, I receive this error. How could this be fixed?
This is kind of type safety warning. String interpolation expects a string convertible object. Any object is swift can be represented as string, but without explicit implementation it'll be information about the class or struct name, address, etc. This is most probably not what you wanna see on your string, that's why this error is there.
You can silence it using String(describing::
struct scoreView: View {
@Binding var scoreTracker: Int
@Binding var gameTracker: Int
@Binding var gameScores: [Any]
@State var counter = 0
var body: some View {
Text("Scores: ")
VStack {
List {
ForEach(1..<gameTracker) {
Text("Game \($0): \(String(describing: gameScores[gameTracker])) ")
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With