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

challenge day 35

Forums > 100 Days of SwiftUI

Sorry for being so stuborn

can some one tell me why does not throw any erros but wont compile, ony says "updating took more tahe 5 seconds"

this is a try for the challenge.

import SwiftUI

struct Question {
    let question: String
    let answer: Int
}

struct ShowQuestions: View {
    var question : Question
    @State private var userAnser = ""

    var body: some View{
        Text(question.question)
        TextField("0", text: $userAnser)
            .frame(width: 100, height: 100)
    }
}

struct QuestionsRow: View {
    @State private var tableOfMult = 1
    @State private var questions = [Question]()

    var body: some View {
        ScrollView {
            ForEach(0 ..< 5, id:\.self) { index in
                HStack {
                    ShowQuestions(question: questions[index])

                }
            }
        }.onAppear(perform: createQuestions)

    }

    func createQuestions(){
        let table = tableOfMult
        var questionset = ""
        var answerset = 0
        for i in (0...12).shuffled(){
            questionset =  "\(table) x \(i)"
            answerset = table * i
            questions.append(Question(question: questionset, answer: answerset))
        }

    }
}

struct QuestionsRow_Previews: PreviewProvider {
    static var previews: some View {
        QuestionsRow()
    }
}

3      

It will not work as the view is produce before the array is made and therefore an empty array so change thing by taking out the func and adding as a computed property.

struct ContentView: View {
    @State private var tableOfMultiple = 1
//    @State private var questions = [Question]()

    var questions: [Question] {
        var newQuestion = [Question]()
        var questionSet = ""
        var answerSet = 0
        for i in (0...12).shuffled() {
            questionSet = "\(tableOfMultiple) x \(i)"
            answerSet = tableOfMultiple * i
            newQuestion.append(Question(question: questionSet, answer: answerSet))
        }

        return newQuestion
    }

    var body: some View {
        ScrollView {
            ForEach(0 ..< 5, id:\.self) { index in
                ShowQuestions(question: questions[index])
            }
        }
//        .onAppear(perform: createQuestions) <- no longer needed
    }
/// Removeed the Function

//    func createQuestions(){
//        let table = tableOfMult
//        var questionset = ""
//        var answerset = 0
//        for i in (0...12).shuffled(){
//            questionset =  "\(table) x \(i)"
//            answerset = table * i
//            questions.append(Question(question: questionset, answer: answerset))
//        }
//    }
}

I have also taking out the HStack and add to the ShowQuestions view

struct ShowQuestions: View {
    var question : Question
    @State private var userAnswer = ""

    var body: some View{
        HStack {
            Text(question.question)
            TextField("0", text: $userAnswer)
                .frame(width: 100, height: 100)
        }
    }
}

3      

i don't understand why it creates firs the view and then call the function?

that was my way of presonalice this code, tu put al the questions the table of multiplication etc

import SwiftUI

struct Question {
    let text: String
    let actualAnswer: String
    var userAnswer = ""

    init(){
        let left = Int.random(in: 1...12)
        let rigth = Int.random(in: 1...10)

        text = "\(left) * \(rigth)"
        actualAnswer = "\(left * rigth)"
    }
}

struct Questionsrow: View {

    var question: Question
    var body: some View{

        HStack{
            Text(question.text)
                .padding([.top, .bottom, .leading])

            ZStack{
                Text("")
                    .padding()
                    .frame(width: 150)
                    .overlay(
                    RoundedRectangle(cornerRadius: 10)
                       .fill(Color.blue)
                )
                Text(question.userAnswer)

            }
     }
        .font(.system(size: 48, weight: .regular, design: .monospaced))
        .foregroundColor(.black)
    }
}

struct ContentView: View {
    @State private var questions = [Question]()

     var body: some View {

         ZStack{
             ForEach(0 ..< questions.count, id: \.self) { index in

                Questionsrow(question: self.questions[index])
                     .offset(x: 0, y: CGFloat(index) * 100)

             }
         }
         .frame(width: 1000, height: 600)
         .onAppear(perform: createQuestion)

     }
     func createQuestion(){

         for _  in 1...50{
             questions.append(Question())
         }
     }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

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.