Hello everyone! I am trying to get back into swift coding by following an example project (more specifically, this one: https://www.youtube.com/watch?v=r1C8HK5_hsc ) but have came across an error when coding the view that displays the array of possible answers. I understand the error to some extent, but am confused on how I'm suppose to conver the Int to a string to display the answers. Can anyone help me out? Thank you so much in advance!
Here is my code:
(The view)
import SwiftUI
struct GameView: View {
@StateObject var viewModel = GameViewModel()
var body: some View {
ZStack {
GameColor.main.ignoresSafeArea()
VStack {
Text(viewModel.progressText)
.padding()
Spacer()
Text(viewModel.questionText)
.font(.title)
.multilineTextAlignment(.center)
.padding()
Spacer()
Spacer()
HStack {
ForEach(0..<viewModel.answerIndices.count, id: \.self) { index in
AnswerButton(text: viewModel.answerIndices[index]) {
viewModel.makeSelectionForCurrentQuestion(at: index)
}
.background(viewModel.colorForButton(at: index))
.disabled(viewModel.selectionWasMade)
}
}
if viewModel.selectionWasMade {
Button(action: viewModel.advanceGameState,
label: {
BottomText(str: "Next")
})
}
}.padding(.bottom)
}
.navigationBarHidden(true)
.background(resultsNavigationLink)
}
private var resultsNavigationLink: some View {
NavigationLink(
destination: ResultsView(viewModel: ResultsViewModel(selectionCount: viewModel.selectionCount, gameStartTime: viewModel.gameStartTime, gameEndTime: Date())),
isActive: .constant(viewModel.gameIsOver),
label: { EmptyView() })
}
}
struct AnswerButton: View {
let text: String
let onClick: () -> Void
var body: some View {
Button(action: {
onClick()
}) {
Text(text)
}
.padding()
.border(Color.blue, width: 4)
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
GameView()
}
}
}
(The following is the model)
import SwiftUI
class GameViewModel: ObservableObject {
@Published var game = Game()
var questionText: String {
game.currentQuestion.questionText
}
var answerIndices: Range<Int> {
game.currentQuestion.possibleAnswers.indices
}
var correctAnswerIndex: Int {
game.currentQuestion.correctAnswerIndex
}
var progressText: String {
"Question \(game.currentQuestionIndex + 1) / \(game.questionCount)"
}
var selectionWasMade: Bool {
game.selections[game.currentQuestion] != nil
}
var selectionCount: (Int, Int) {
game.selectionCount
}
var gameIsOver: Bool {
game.gameIsOver
}
var gameStartTime: Date {
game.startTime
}
func answerText(for index: Int) -> String {
game.currentQuestion.possibleAnswers[index]
}
func advanceGameState() {
game.advanceGameState()
}
func makeSelectionForCurrentQuestion(at index: Int) {
game.makeSelection(at: index)
}
func colorForButton(at buttonIndex: Int) -> Color {
guard let selectedIndex = game.selections[game.currentQuestion], selectedIndex == buttonIndex else { return .clear }
if selectedIndex == correctAnswerIndex {
return .green
} else {
return .red
}
}
}
(Finally-- here is a possible question)
import Foundation
struct Question: Hashable {
let questionText: String
let possibleAnswers: [String]
let correctAnswerIndex: Int
static let allQuestions: [Question] = [
Question(questionText: "What was the first computer bug?",
possibleAnswers: [
"Fly",
"Moth",
"Ant",
"Beetle",
],