BLACK FRIDAY: Save 50% on all my Swift books and bundles! >>

challenge day 35

Forums > SwiftUI

I have made this structs becouse i want to do a global model. In the initial view the user enters al the data, the you push the start button and go to the question rowview

struct Question {
    let problem: String
    let answer: String

}

struct Game {
    let score: Int = 0
    var numberOfCuestions: [String] = ["5", "10", "20", "All"]
    var tableOfMultip: Int = 0
    var problem: String = ""
    var product: String = ""

   mutating func createTableOfMult(table a: Int) -> [Question]{
        var question = [Question]()

        for i in 1...12{
            problem = "(\(a) * \(i))"
            product = "\(a * i)"
        question.append(Question(problem: problem, answer: product))

        }
        return question
    }
}

but when i use on to create the cuestion row in a separate view i get the error : No exact matches in call to initializer

import SwiftUI

struct QuestionRow: View {
    @Binding var game: Game
    @State private var  answer = ""
    var body: some View {

        ZStack {

            HStack {
                Text(game.createTableOfMult(table: game.tableOfMultip)) //No exact matches in call to initializer 
                    .padding([.top, .leading, .bottom])
                TextField(/*@START_MENU_TOKEN@*/"Placeholder"/*@END_MENU_TOKEN@*/, text: $answer)
                    .frame(width: 100, height: 100)
//                    .background(questions)
            }
            .font(.system(size: 48, weight: .regular, design: .monospaced))

        }

    }

}

struct QuestionRow_Previews: PreviewProvider {
    static private var game = Binding.constant(Game())
    static var previews: some View {

        QuestionRow(game: game)
    }
}

does someone know why?

3      

Text requires a String, which could be characters or a string interpolation.

If you change

Text(game.createTableOfMult(table: game.tableOfMultip))

to

Text("\(game.createTableOfMult(table: game.tableOfMultip))")

You will now get a different error, because what game.createTableOfMult is providing is a [Question] not a string.

Providing some way for Text to be able to handle [Question] would be a way forward. Possibly writing an extension to Text.

3      

i dont know how to do that, and i have search

3      

Save 50% in my WWDC sale.

SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.