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

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      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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.