Hello,
I am trying to have an array of ViewModel
in a ForEach
, but the following error is thrown:
Type 'any ViewModel' cannot conform to 'Identifiable'
A similar error would happen if instead of [any ViewModel]
, I'd have [any Identifiable<UUID>]
:
Type 'any Identifiable < UUID >' cannot conform to 'Identifiable'
I am not sure I understand why this happens as ViewModel
is tied to Identifiable<UUID>
, so the elements of the collection passed to ForEach
will always have the same type of id
.
Can somebody please explain to me what I'm missing?
Thank you.
protocol ViewModel: Identifiable<UUID>, ObservableObject {}
class ViewModel1: ViewModel {
let id = UUID()
}
class ViewModel2: ViewModel {
let id = UUID()
}
let viewModels: [any ViewModel] = [
ViewModel1(),
ViewModel2()
]
struct ListScreen: View {
var body: some View {
List {
ForEach(viewModels) { model in // <- Error happens here
// ...
}
}
}
}