How do you fix "Type cannot conform to View" in SwiftUI?

← Swift answers

The body (or a closure inside it) produced a value that is not a View, often () because of a statement such as print or an assignment. Return a view from every path.

SwiftUI's builders only accept views. Anything else, including a plain statement that returns nothing, ends up as the type the error complains about.

// Error: Type '()' cannot conform to 'View'
var body: some View {
    VStack {
        print("rendering")   // a statement is not a view
        Text("Hello")
    }
}
 
// Fix: run side effects outside the builder, or use a throwaway binding
var body: some View {
    VStack {
        let _ = print("rendering")
        Text("Hello")
    }
    .onAppear { print("appeared") }
}
  • Make sure the struct really conforms to View and that body is declared as some View.
  • Every branch of an if or switch must produce a view; a branch that returns nothing breaks the type.
  • A ForEach or Button closure that does only work, without returning a label, causes the same message.
  • A mistake lower in the file can surface here, so comment out parts of the body until the error moves.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required