TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Cannot convert value of type 'Int' to expected argument type 'String'

Forums > Swift

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",
             ],

3      

You can do it a number of ways

VStack {
    // String interpolation
    Text("\(number)")

    // Number formatting
    Text(number, format: .number)

    // Type casting
    Text(String(number))
}
.padding()

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.