I am trying to display multiple previews of the card view (Apple's SwiftUI tutorial). I managed to do it manually using the Group
struct.
struct CardView_Previews: PreviewProvider {
static var previews: some View {
Group {
CardView(scrum: DailyScrum.data[0])
.background(DailyScrum.data[0].color)
CardView(scrum: DailyScrum.data[1])
.background(DailyScrum.data[1].color)
}.previewLayout(.fixed(width: 400, height: 60))
}
}
I would like to automate the display of four cells based on test data with a ForEach, but the following fails to build and the compiler fails to produce a diagnostic for the expression.
struct CardView_Previews: PreviewProvider {
static var previews: some View {
Group {
ForEach(DailyScrum.data) { scrum in
CardView(scrum: scrum)
.background(scrum.color)
}
}.previewLayout(.fixed(width: 400, height: 60))
}
}
Is there a way to construct the previews without explicitly creating each preview ?
I found the workaround I was looking for by simply adding the id
argument to the ForEach
initializer. It prevents from having to add Identifiable
conformance to the type :
struct CardView_Previews: PreviewProvider {
static var previews: some View {
Group {
ForEach(DailyScrum.data, id: \.title) { scrum in
CardView(scrum: scrum)
.background(scrum.color)
}
}.previewLayout(.fixed(width: 400, height: 60))
}
}