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

can someone explain this code

Forums > SwiftUI

this code is par of this video https://www.youtube.com/watch?v=FE4ys3tW1VI&list=LL&index=2&t=1545s

and i dont know why xcode doesnot give an error but it does not show anything.

and other thing i need explanation ( this is why i made the post) is this line f code and how it works.

@State private var questions = [Question]()

is an array of struct???

when they use the append method is every question is set to a particular propertie in the struc question??

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])
        }
        .font(.system(size: 48, weight: .regular, design: .monospaced))
        .foregroundColor(.white)
    }
}

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      

You are correct. that line of code is declaring an array of Question

This line then gets called when the view appears and calls the createQuestion function

 .onAppear(perform: createQuestion)

The createQuestion function then creates 50 questions (with default values) and appends each one into the array. The default values can be seen in the Question structs initialiser. In this example, it is taking a random value between 1 and 12 then multiplying it with another random value between 1 and 10. It then sets text variable and actual answer variable to the result.

So are you saying that you've got this code and nothing at all is displayed?

Rich

4      

figured out why it doesn't show anything. :-)

White text, white background:

 .foregroundColor(.white)

remove that line and it should show

4      

I have change to color to black and nothing the dot in the simulator is red.

and for the array struct...whe the function createQuestion in the for loop the positions for example

pos text ansewer

  1. "4*3" "12"
  2. "5*5". "25"

and so on???

can I put print(question.text[1])???

or how can i read question.text or question.actualAnswer???

what i don't get is that in a normal array (for me XD) var a = [String] is only a value in this struct yo have three properties how the append method store the propertie...

3      

oh yes, there was one other thing I needed to do.

you missed a full stop (period) in front of your frame modifier.

if you add that then it will stop crashing and the simulator will work

3      

now works.

But if some one can explain to me the array struct it will be awsome!

3      

I think i have get it

It creates the array of queation struc

then in conten view it loops over questionRow

In QuestionRow when it declares var question: Question yo pass there the information over the position 1 (for example)

it calls Text(question.text) in position one!!!!

3      

yes

In the ForEach, it's looping through the questions array and for each question in the array, it passes the specific question (e.g. question in position 1) into the Question Row. A Question Row is then created from that question.

So you can add Text(question.actualAnswer) into your Quesiton Row to show the actual answer.

There's a couple of ways of displaying the "position". One way is to add a position variable into questionsRow. It should be an Int and you can call it position.

Then change this:

  Questionsrow(question: self.questions[index])

to:

  Questionsrow(question: self.questions[index], position: index + 1)

then in your QuestionsRow:

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

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.