Having troubles putting down together SwiftUI and generic types for handling Core Data.
Consider following example:
Parent
is abstract. Foo
and Bar
are children of Parent
and they have some custom attributes.
Now what I want to do, is roughly that:
protocol EntityWithView {
associatedtype T: View
func buildView() -> T
}
extension Parent: EntityWithView {
func buildView() -> some View {
fatalError("Re-implement in child")
}
}
extension Foo {
override func buildView() -> some View {
return Text(footribute)
}
}
extension Bar {
override func buildView() -> some View {
return Text(atrribar)
}
}
struct ViewThatUsesCoreDataAsModel: View {
let entities: [Parent]
var body: some View {
ForEach(entities) { entity in
entity.buildView()
}
}
}
I would want to add polymorphic builder to my core data entities that shape data or build views, that confirm to common interface so I can use them without casting/typing.
Problem that compiler throws errors if I try to modify generated Core data entity directly not through extension, and confirming to protocol though extension doesn't allow overriding.