I have defined a CustomProtocol
, which requires a unique identifier. I have also created a CustomModel
that implements this protocol. Despite having an id property as required by the Identifiable
protocol, I am unable to use CustomModel
as an identifiable type in SwiftUI.
protocol CustomProtocol: Identifiable {
var id: String { get }
}
struct CustomModel: CustomProtocol {
let id = UUID().uuidString
}
class CustomModelStore: ObservableObject {
@Published var models: [any CustomProtocol] = []
init() {
models = Array(repeating: CustomModel(), count: 10)
}
}
struct CustomProtocolView: View {
@StateObject var store = CustomModelStore()
@State var selectedModel: (any CustomProtocol)?
var body: some View {
VStack {
ForEach(store.models) { model in
Text(model.id)
.font(.footnote)
.onTapGesture {
selectedModel = model
}
}
.sheet(item: $selectedModel) { model in
Text(model.id)
.font(.subheadline)
}
}
}
}
struct CustomProtocolView_Previews: PreviewProvider {
static var previews: some View {
CustomProtocolView()
}
}
Of course I can specify id
in ForEach
, but this way doesn't acceptable for me because I have no chance do it in .sheet
or .fullScreenCover
view modifiers. In my store I also can't change type from CustomProtocol
to CustomModel