UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

challenge 35

Forums > 100 Days of SwiftUI

I am trying to solve the problem but im stuc at the model of my program, i have search for solutions and every one semms todo somethng like this

struct Task {
    var task: String
    var answer: String
}

and then declare

var tasks = [Task]()

and i don't understand that sentence [Task] is an array of the struct??? how can i read or chancge the properties taks or answer??

thanx

3      

You can read write depending on what you want to do. This is a small example

struct ContentView: View {
    @State private var tasks = [Task]()

    @State private var inputTask = ""
    @State private var inputAnswer = ""

    var body: some View {
        VStack {
            TextField("Task", text: $inputTask)
            TextField("Answer", text: $inputAnswer)

            Button("Add", action: add)

            List {
                ForEach(tasks) { task in
                    HStack {
                        Text(task.task) // Reading the tasks array
                        Text(task.answer)
                    }
                }
            }
        }
        .padding()
    }

    func add() {
        let task = Task(id: UUID(), task: inputTask, answer: inputAnswer)
        tasks.append(task) // writing to tasks array
    }
}

3      

it is kind of a array of a tupple?? I do not really now how it works

can i do???

for i in  1...12{
print(task.task[i])
}

this was my initial struct to solve the problem

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
    }
}

and this is the view i want to create to do the question rows

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)
    }
}

my problem is how i read a value from the question array creates in the fuction. Becoues the function returns [Questions] thar array is the same or should i do something like...

var questionAsk = game.createTableOfMult(table: game.tableOfMultip)

so in that way i can use

questionAsk.game.question???

i dont know if i have explain what i want correctly.

thnax

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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

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.