Issue #791
SwiftUI has View protocol which represents part of your app’s user interface and provides modifiers that you use to configure views.
You create custom views by declaring types that conform to the View protocol. Implement the required body computed property to provide the content for your custom view.
A typical example of View is using struct
enum HeroType {
case melee
case range
}
struct ContentView: View {
let heroType: HeroType
var body: some View {
switch heroType {
case .melee: Text("Melee")
case .range: Text("Range")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(heroType: .melee)
}
}
But were are not limited to using struct, we can use enum or class if we need to. For example here I use enum that has body
method
enum HeroView: View {
case melee
case range
var body: some View {
switch self {
case .melee: Text("Melee")
case .range: Text("Range")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
HeroView.melee
}
}
Note that if you use class
final class ContentView: View {
}
SwiftUI will crash because it expects only value types like struct and enum
Fatal error: views must be value types