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
Viewand thatbodyis declared assome View. - Every branch of an
iforswitchmust produce a view; a branch that returns nothing breaks the type. - A
ForEachorButtonclosure 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.
