|
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??
|
|
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
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 |
|
figured out why it doesn't show anything. :-) White text, white background:
remove that line and it should show |
|
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
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... |
|
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 |
|
|
|
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!!!! |
|
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:
to:
then in your QuestionsRow:
|
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.
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.
Link copied to your pasteboard.