This error usually appears when you’re building a SwiftUI view and accidentally include a line of code that doesn’t return a view. SwiftUI’s body property must return a valid View. If you insert logic like print() or variable assignments directly inside the view hierarchy, the compiler throws this error.
Let’s break down what causes it, what the error message really means, and how to fix it properly.
What Does Type ‘()’ cannot conform to ‘View’ Mean?
The error – Type ‘()’ cannot conform to ‘View’ is better understood when broken down. Here is a quick look:
- () is Swift’s shorthand for Void, which is the return type of functions or expressions that don’t return a meaningful value.
- ‘View‘ is the protocol SwiftUI expects your body to return.
To put it in simple words, the error is telling you that you returned a Void, but I need a View.
Why Does It Happen?
SwiftUI’s body property is a computed property that must return a view (like Text, VStack, HStack, etc.). If you insert something like a print() statement or assign a value inside the view hierarchy, Swift interprets that line as returning Void—which doesn’t conform to View.
Example That Causes the Error
In the below example, the HStack expects its children to be views. But print(“View loaded”) returns Void, which breaks SwiftUI’s expectations.
struct MyView: View {
var body: some View {
HStack {
Text("Hello")
print("View loaded") // This line returns Void (())
}
}
}
How to Fix the Error?
There are several ways to resolve this error — all based on removing or relocating the non-view logic. Here are the most practiced approaches, along with examples.
1. Move Side Effects to .onAppear: If you need to run code when the view loads (like setting a state value), use .onAppear.
struct ContentView: View {
@State var name: String = ""
var body: some View {
Text(name)
.onAppear {
name = "Angela" // Runs only when the view appears
}
}
}
2. Initialize State Outside body: If your state doesn’t need to be changed dynamically, initialize it when declaring it.
@State var name: String = "Angela"
var body: some View {
Text(name)
}
3. Use Conditional Logic That Returns Views: SwiftUI allows conditional statements as long as every code path returns a view.
var body: some View {
if isLoggedIn {
Text("Welcome")
} else {
Text("Please log in")
}
}
4. Move Non-View Logic to a Separate Function: This approach keeps your body focused on layout, while your logic stays outside.
func updateName() {
name = "Angela"
}
var body: some View {
Text(name)
.onAppear {
updateName()
}
}
This SwiftUI error can be confusing, especially if you’re new to the framework. But the fix is usually straightforward: only return views from your body, and move all side effects to .onAppear, external functions, or lifecycle handlers.
If you’re seeing Type ‘()’ cannot conform to ‘View’, check your layout code — and make sure every expression in the view builder returns a SwiftUI view.
var body: some View {
if isLoggedIn {
Text("Welcome")
} else {
Text("Please log in")
}
}