Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How could I stuff assertions into viewbuilders?

        AsyncImage(url: url) { phase in
            switch phase {
            case .empty:
                ProgressView()
            case .success(let image):
                imageProcessed(image: image)
            case .failure:
                Image(systemName: "photo")
            @unknown default: assertionFailure()
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this yields Type '()' cannot conform to 'View'
                // Since the AsyncImagePhase enum isn't frozen,
                // we need to add this currently unused fallback
                // to handle any new cases that might be added
                // in the future:
                EmptyView()
     ^^^^^^^^^^^^^^^^^ adding return here yields Cannot use explicit 'return' statement in the body of result builder 'ViewBuilder'
            }
        }
like image 462
Anton Tropashko Avatar asked Nov 19 '25 17:11

Anton Tropashko


1 Answers

There's a trick that is often used to print within a View body that you can use:

let _ = assertionFailure()

By doing a "fake" variable assignment like this, the result builder doesn't complain about syntax errors.

like image 173
jnpdx Avatar answered Nov 21 '25 08:11

jnpdx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!