Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to create a View that renders different content based on enum cases in Swift UI
enum Block {
case h(title: String)
case p(body: String)
}
extension View {
func erased() -> AnyView {
return AnyView(self)
}
}
extension Block : View {
var body: AnyView {
switch self {
case let .h(title):
return
HeadingView(title: title)
.erased()
case let .p(body):
return
ParagraphView(text: body)
.erased()
}
}
}
struct CardContentView : View {
let content: Content
var body: some View {
VStack(alignment: .leading) {
ForEach(content.body) {item in
item
}
}
.padding([.leading, .bottom, .trailing])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment