let _ = print("value"), or create a reusable Print view extension that returns EmptyView after logging.Why this happens
SwiftUI view bodies are built with @ViewBuilder, a result builder that transforms the closure’s content into a view hierarchy. The builder expects every expression in the closure to produce a View (or something that can become one). A bare print() call returns Void, which the builder tries to incorporate into the view tree and fails with errors like “Closure containing a declaration cannot be used with result builder ‘ViewBuilder’” or “String is not convertible to any View”.
Result builders intentionally reject side-effect-only statements to keep view construction pure and predictable. The builder has no concept of execution order—views may be created, discarded, or recreated at any time—so relying on print() inside the body for debugging is fragile even if it compiled.
How to fix it
1. Assign to a wildcard (quickest fix)
Wrap the print() call in a let _ = assignment. The wildcard _ discards the Void return value before the view builder sees it, so the builder only receives valid view expressions.
var body: some View {
NavigationView {
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Favorite only")
}
ForEach(landmarkData) { landmark in
let _ = print("landmark: \(landmark)")
if !userData.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
}
}
.navigationTitle("Landmarks")
}
}
Note:
NavigationButtonis deprecated; useNavigationLinkin current SwiftUI.
This works anywhere inside a view builder—inside ForEach, if branches, or the top level of body.
2. Create a reusable Print view helper (cleaner for repeated use)
Add a small View extension that prints its arguments and returns EmptyView. This keeps the call site readable and works naturally inside view builders.
extension View {
func Print(_ items: Any..., separator: String = " ", terminator: String = "\n") -> some View {
for item in items {
print(item, separator: separator, terminator: "")
}
print(terminator: terminator)
return EmptyView()
}
}
Usage inside any view body:
ForEach(landmarkData) { landmark in
Print("landmark:", landmark)
if !userData.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
}
Because EmptyView is a valid view, the builder accepts it without complaint. Wrap multiple Print calls in a Group if you need them adjacent to other views without extra containers.
3. Use the Xcode debugger instead (best practice)
For anything beyond quick sanity checks, set a breakpoint (⌘\ on the line) and use the debug console (po landmark, p landmark.id, etc.). Breakpoints pause execution at a known point, let you inspect the full state, and don’t clutter source code with temporary logging.
How to prevent it
- Default to breakpoints for interactive debugging; they’re faster and more powerful.
- Keep
print()out of view bodies—move logging to model changes,onAppear/onChange, or view model methods where execution order is guaranteed. - Remove or gate debug logs with
#if DEBUGbefore shipping. - Adopt the
Printhelper if you frequently need inline logging during UI prototyping.
FAQ
Why does let _ = print() work but bare print() doesn’t?
The view builder only processes expressions that appear as statements in the closure. let _ = print() is a declaration (a pattern-binding), not an expression statement, so the builder ignores its initializer’s Void result. A bare print() is an expression statement whose Void value the builder tries to turn into a view.
Can I use print() inside onAppear or task modifiers?
Yes. Those modifiers accept closures that run at well-defined lifecycle moments, not view builders. print() works normally there because the closure isn’t processed by @ViewBuilder.
Is there a performance cost to the Print helper?
Negligible. EmptyView has zero layout cost, and the print calls only execute when the view body is evaluated—which happens during rendering anyway. In release builds, wrap the helper in #if DEBUG to eliminate it entirely.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.