SegmentedControl + ForEach broken in beta 4

SegmentedControl has this weird behaviour in beta 4:


The following does work and allows player A, B or C to be selected:


var players = {

[Player("A"), Player("B"), Player("C")]

}


SegmentedControl(selection: $game.master) {

// BTW, why does beta 4 require players to have the extra ()?

ForEach(0 ..< self.players().count) {

Text(self.players()[$0].name).tag($0)

}

}


The following does allow player A, B or C to be selected, or none:


SegmentedControl(selection: $game.master) {

ForEach(0 ..< self.players().count) {

Text(self.players()[$0].name).tag($0)

}

Text("none").tag(self.players().count)

}


The following does allow player B or C to be selected, but when trying to select A, the selection switches to "none" (you have to set a breakpoint to see the flip between A and "none"):


SegmentedControl(selection: $game.master) {

Text("none").tag(0)

ForEach(0 ..< self.players().count) {

Text(self.players()[$0].name).tag($0+1)

}

}

Replies

// BTW, why does beta 4 require players to have the extra ()?


You should add () at the end of:


var players = {
    [Player("A"), Player("B"), Player("C")]
}()


Otherwise, players is a closure, not the array resulting from execution of closure.


Why do you add

    Text("none").tag(0)

// Why do you add

Text("none").tag(0)


To be able to also select no player at all. Once you have selected an element, SegmentedControl no longer allows to unselect that element AFAIK. (Nor does it allow multi-element selection BTW, like UIKit previously.)


// You should add () at the end


Yes of course, thanks!