I have a List loading from an array of protocol conforming types. When I add a selection binding to the List the compiler won’t longer compile the code.
The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
When I try using a Set instead of an Array I get an error.
Type ‘any IRow’ cannot conform to ‘Hashable’
protocol IRow: Identifiable, Hashable {
var id: UUID { get }
var rowType: String { get }
}
struct TextItemRow: IRow {
let id = UUID()
let rowType = "text"
var text: String = ""
}
struct HeadlineItemRow: IRow {
let id = UUID()
let rowType = "headline"
var text: String = ""
}
struct Item {
let id = UUID()
var rows: [any IRow] = [HeadlineItemRow(text: "H_mock"), TextItemRow(text: "T_mock")]
}
struct ContentView: View {
let item = Item()
@State private var selection: [any IRow] = []
var body: some View {
VStack {
List(selection: $selection) {
ForEach(item.rows, id: .id) { row in
if let r = row as? HeadlineItemRow {
Text("(r.id.uuidString) : (r.text)")
.tag(row)
} else if let r = row as? TextItemRow {
Text("(r.id.uuidString) : (r.text)")
.tag(row)
}
}
}
Text("")
}
.frame(width: 250)
}
}