{"id":1005,"date":"2025-06-05T14:57:43","date_gmt":"2025-06-05T12:57:43","guid":{"rendered":"https:\/\/mic.st\/blog\/?p=1005"},"modified":"2025-06-05T14:57:47","modified_gmt":"2025-06-05T12:57:47","slug":"cannot-use-explicit-return-statement-in-the-body-of-result-builder-viewbuilder-fixing-guide","status":"publish","type":"post","link":"https:\/\/mic.st\/blog\/cannot-use-explicit-return-statement-in-the-body-of-result-builder-viewbuilder-fixing-guide\/","title":{"rendered":"Cannot use explicit &#8216;return&#8217; statement in the body of result builder &#8216;ViewBuilder&#8217; fixing guide"},"content":{"rendered":"\n<p>And another post on how to fix crude Xcode or more specifically SwiftUI errors. Did you ever come across &#8220;Cannot use explicit &#8216;return&#8217; statement in the body of result builder &#8216;ViewBuilder'&#8221;? I see it regularily and it&#8217;s actually not to big of a deal to fix it once you see what is usually causing it.<\/p>\n\n\n\n<p>To me the value of debugging information of this specific error is as low as in this one I also wrote a post about <a href=\"https:\/\/mic.st\/blog\/swiftui-and-the-compiler-is-unable-to-type-check-this-expression-in-reasonable-time\/\">https:\/\/mic.st\/blog\/swiftui-and-the-compiler-is-unable-to-type-check-this-expression-in-reasonable-time\/<\/a>. 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. <\/p>\n\n\n\n<p>Okay, now let&#8217;s see how to actually fix this.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"25\" src=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2025\/06\/image.png\" alt=\"\" class=\"wp-image-1006\" srcset=\"https:\/\/mic.st\/blog\/wp-content\/uploads\/2025\/06\/image.png 730w, https:\/\/mic.st\/blog\/wp-content\/uploads\/2025\/06\/image-300x10.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><figcaption class=\"wp-element-caption\">The &#8220;Cannot use explicit &#8216;return&#8217; statement in the body of result builder &#8216;ViewBuilder'&#8221; error in the wild. Or as seen inside of Xcode.<\/figcaption><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\">Wrong or not updated parameter in Previews<\/h2>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>For example let&#8217;s say you had a parameter that did have a parameter named<code>accessibilyIDs<\/code> expecting a special type containing accessibility identifiers.<\/p>\n\n\n\n<p>You decided, &#8220;It is overengineered, I don&#8217;t need that anymore&#8221; and changed your initializer to <code>accessibilyID<\/code> with a basic <code>String<\/code> type.<\/p>\n\n\n\n<p>Now, if you have a couple of <code>View<\/code>s created and returned inside of your <code>#Preview<\/code> macro it will just fail with &#8220;<code>Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder'<\/code>&#8221; pointing to that specific line where the preview does return the <code>View<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Similar to a lot of other Xcode errors, it is one of these where you just &#8220;have to know&#8221;. 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.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>And another post on how to fix crude Xcode or more specifically SwiftUI errors. Did you ever come across &#8220;Cannot use explicit &#8216;return&#8217; statement in the body of result builder &#8216;ViewBuilder&#8217;&#8221;? I see it regularily and it&#8217;s actually not to big of a deal to fix it once you see what is usually causing it.&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[3],"tags":[4,35,61,15],"class_list":["post-1005","post","type-post","status-publish","format-standard","hentry","category-ios-development","tag-ios","tag-swift","tag-swiftui","tag-xcode"],"_links":{"self":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/1005","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/comments?post=1005"}],"version-history":[{"count":1,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/1005\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/posts\/1005\/revisions\/1008"}],"wp:attachment":[{"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/media?parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/categories?post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mic.st\/blog\/wp-json\/wp\/v2\/tags?post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}