I have a view that is initialized with a list of items, and then during the initialization process, we need to pick one item at random. Something like this:
struct ItemsView: View {
    var items:[Item]
    @State var current:Item?
    init(items:[Item] = []) {
        self.items = items
        if items.count > 0 {
            let index = Int.random(in: 0..<items.count)
            self.current = items[index] // doesnt work
        }
        if current != nil {
            print("init with", self.items.count, "items. Chose", current!)
        }
    }
    // ...
}
The job of the ItemsVew is to show one item at time, at random, so we start by picking an item at random, but self.current = items[index] literally doesn't do anything as far as I can tell, for reasons I don't understand. So either I am doing something silly, or I am thinking about how to solve this in an incorrectly somehow.
So how do you initialize a State variable in the init function. I have tried:
self.current = State(initialValue: items[index])
But that simply triggers a compiler error. So how do we select an initial item that will be used when the view is displayed?
Cannot assign value of type 'State<Item>' to type 'Item'
What am I doing wrong?
 
     
    