Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@darknoon
Created June 12, 2019 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darknoon/b5e54c2ce5b211dc717c37061a47f09f to your computer and use it in GitHub Desktop.
Save darknoon/b5e54c2ce5b211dc717c37061a47f09f to your computer and use it in GitHub Desktop.
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