Learn Courses My Dashboard

Beginner fails to shuffle an array inside body

Hi! I just completed the 14-day YouTube course and decided to try my skills by making a simple quiz game. I put my questions in an array and want to shuffle them so that the order changes with each run. But when I try to use shuffle() I get an error message: "Type ‘()’ cannot conform to ‘View’

import SwiftUI

struct ContentView: View {
@State private var myTable = [[“a1”,“a2”],[“b1”,“b2”],[“c1”,“c2”]]

var body: some View {
    var b = myTable.shuffle()
    print(b)
    
}

}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Itself the shuffle() seems to work. I can make it work if I put the shuffle inside a function and run it through an alert-window (I just left it out in the example to make it simple). However, if I call the same function inside body, I’ll get the same error. Anything with empty parentheses () seem to cause problems. What am I not getting?

What am I not getting?

Don’t put logic inside the body.

Why do you want to shuffle your array? Figure that out and then place it accordingly. A computed property, a function, a property on an ObservedObject, whatever. But that kind of code does not belong in the body.