ios – Easy methods to stop array from being continuously shuffled


I am a n00b and operating into an issue i can not seem to clear up. Hope you guys/ladies may also help me repair it.

I’m constructing a examination app in Swift and have an array of solutions per query acquired through an API. I need the solutions to be shuffled. I’m reaching this as follows:

if let solutions = currentQuestion()?.api_question.api_answers {
                                @State var shuffledAnswers = solutions.shuffled()
                                ForEach(shuffledAnswers, id: .id_answer) { reply in
                                    Button(motion: {
                                        selectedAnswers[currentQuestionIndex] = reply
                                    }) {
                                        HStack {
                                            Textual content("(answerLetter(for: reply)):")
                                            Textual content(reply.reply)
                                                .foregroundColor(.black)
                                                .body(maxWidth: .infinity, maxHeight: .infinity, alignment: .main)
                                                .lineLimit(nil)
                                                .fixedSize(horizontal: false, vertical: true)
                                        }
                                        .padding()
                                        .background(
                                            RoundedRectangle(cornerRadius: 10)
                                                .fill(Coloration.white)
                                                .shadow(colour: Coloration.grey.opacity(0.3), radius: 4, x: 0, y: 2)
                                        )
                                        .overlay(
                                            RoundedRectangle(cornerRadius: 10)
                                                .stroke(selectedAnswers[currentQuestionIndex] == reply ? Coloration.yellow : Coloration.blue, lineWidth: 2)
                                        )
                                    }
                                    .padding(.vertical, 8)
                                }
                            }

Now this works, because the solutions are shuffled. Nonetheless, each time a solutions is chosen, the solutions get shuffled once more. Whenever you go to the following query and again, the solutions are shuffled once more. How am i able to stop this from taking place?

I’ve tried the next:

struct PracticeQuestionView: View {
    // ... (different variables and physique content material)

    @State personal var shuffledAnswers: [ApiAnswer] = []
    
    // ... (different code)
    
    var physique: some View {
        // ... (different view content material)
        
        VStack(alignment: .main, spacing: 20) {
            Textual content(currentQuestion()?.api_question.query ?? "Vraag niet gevonden")
                // ... (different code)
            
            if let solutions = currentQuestion()?.api_question.api_answers {
                if shuffledAnswers.isEmpty {
                    shuffledAnswers = solutions.shuffled()
                }
                
                ForEach(shuffledAnswers, id: .id_answer) { reply in
                    Button(motion: {
                        selectedAnswers[currentQuestionIndex] = reply
                    }) {
                        // ... (reply view code)
                    }
                    .padding(.vertical, 8)
                }
            }
            
            // ... (different code)
        }
        
        // ... (different code)
    }
    
    // ... (different code)
}

The issue right here is that i’m getting the next error:
Kind '()' can't conform to 'View'

Which i’m unable to unravel.

Any assistance is welcome, if extra data is required, i’ll present it.
Thanks upfront in your time!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles