HomeiOS Developmentios - The best way to stop array from being consistently shuffled

ios – The best way to stop array from being consistently shuffled


I’m constructing a examination app in Swift and have an array of solutions per query acquired through an API. I would like 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(Colour.white)
                                                .shadow(shade: Colour.grey.opacity(0.3), radius: 4, x: 0, y: 2)
                                        )
                                        .overlay(
                                            RoundedRectangle(cornerRadius: 10)
                                                .stroke(selectedAnswers[currentQuestionIndex] == reply ? Colour.yellow : Colour.blue, lineWidth: 2)
                                        )
                                    }
                                    .padding(.vertical, 8)
                                }
                            }

Now this works, because the solutions are shuffled. Nevertheless, at any time when a solutions is chosen, the solutions get shuffled once more. Whenever you go to the subsequent query and again, the solutions are shuffled once more. How am i able to stop this from occurring?

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:
Sort '()' can't conform to 'View'

Which i’m unable to resolve.

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

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments