View type eraser
Example:
//
// TestAnyView.swift
// TestMVVM
//
// Created by dph on 2020/12/2.
//
import SwiftUI
struct TestAnyViewContent: View {
private var click: Bool
init(click: Bool) {
self.click = click
}
var body: some View {
// method 1
if click {
Image(systemName: "pencil")
} else {
Text("Better luck next time")
}
// method 2
Group {
if click {
Image(systemName: "pencil")
} else {
Text("Better luck next time")
}
}
// method 3
if click {
AnyView(Image(systemName: "pencil"))
} else {
AnyView(Text("Better luck next time"))
}
}
}
struct TestAnyView: View {
@State private var click: Bool = Bool.random()
var body: some View {
VStack {
Button("Show") {
click = Bool.random()
}
TestAnyViewContent(click: click)
}
}
}