Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to increment a variable used in ForEach SWIFTUI

ForEach(questionsManager.answerChoices, id:\.id) { answer in
     AnswerRow(index: i, answer: answer).environmentObject(questionsManager)
}

"i" is a @State variable in my View Struct that is assigned to zero

I want the index of each AnswerRow to be set from 0 to 3, how to manage that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Rather than the elements iterate the indices of the array

ForEach(questionsManager.answerChoices.indices, id: \.self) { i in
     AnswerRow(index: i, answer: questionsManager.answerChoices[i]).environmentObject(questionsManager)
}

The @State variable is actually not needed.

Side note: As you pass the questionsManager in the environment the answer parameter is not needed either.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading