And another post on how to fix crude Xcode or more specifically SwiftUI errors. Did you ever come across “Cannot use explicit ‘return’ statement in the body of result builder ‘ViewBuilder'”? I see it regularily and it’s actually not to big of a deal to fix it once you see what is usually causing it.
To me the value of debugging information of this specific error is as low as in this one I also wrote a post about https://mic.st/blog/swiftui-and-the-compiler-is-unable-to-type-check-this-expression-in-reasonable-time/. It basically reads like the compiler gave up and now it is your turn to find out where to look and what to look for.
Okay, now let’s see how to actually fix this.

Wrong or not updated parameter in Previews
Previews are a great thing but you have to make sure of course they do also compile correctly. And here we already have the location to look for. Atl least in my case, it is in around 90% of all cases somewhere in the Previews I did not update.
For example let’s say you had a parameter that did have a parameter namedaccessibilyIDs
expecting a special type containing accessibility identifiers.
You decided, “It is overengineered, I don’t need that anymore” and changed your initializer to accessibilyID
with a basic String
type.
Now, if you have a couple of View
s created and returned inside of your #Preview
macro it will just fail with “Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder'
” pointing to that specific line where the preview does return the View
.
Conclusion
Similar to a lot of other Xcode errors, it is one of these where you just “have to know”. But as SwiftUI error handling got better over the years, I hope errors like this inside of previews will also get a little more easy to see at some point.