[Help] ViewBuilder and SwiftUI issue (Swift 5.3)

Hello,

Following the advice from the great John Sundell ( How Swift 5.3 enhances SwiftUI’s DSL | Swift by Sundell ) a colleague and I were trying to refactor some code to take advantage of new features in Swift 5.3 and take advantage of the view builder changes that would allow us to remove some of the AnyView casting we were doing.

The application is a mixed iOS and macOS app compiled with Xcode 12.0.1 on macOS (Catalina [my coleague] and Big Sur beta 9 [me]).

We noticed that this will not work unless we manually add the @ViewBuilder annotation before of the body property and we thought this would not be needed as it was treated as a special case and inherit the ViewBuilder was inferred by the compiler ( What's new in Swift - WWDC20 - Videos - Apple Developer ):

As you can see only one of the two cases compiles. Are we missing something obvious or bloody obvious :)?

Kind Regards,

Goffredo

Hello everyone, sorry for bumping the topic (won't repeat it), but does anyone spot the stupid mistake I am likely to have made in the original post of this thread? Quite confused :/..

Since you don't provide much to work with, I'd instead ask, does this compile on your env?

struct Test: View {
    let value: Int?

    var body: some View {
        if case let value? = value {
            Text("\(value)")
        } else {
            Text("Test")
        }
    }
}

Can you slowly work your way up to your end-result, and see where it starts to fail? This compiles fine on Xcode 12.0.1 (12A7300).

1 Like

Hello @Lantua, sorry for not sharing more info earlier on.

Without sharing the entire project (small side project, but need to clean it up from company specific things) we are using Xcode 12.0.1 (compiling it on a Big Sur beta 9 Mac at the moment), I am happy to share more just need to understand a bit better what is needed.

I tried to drop your example in it and it does not compile in the existing project. It does compile in a brand new iOS only project, but it does not compile in a brand new macOS project (shared sample iOS and macOS projects): iCloud

Not sure why it would not work for macOS, there could be the stupid thing I was missing and I should have explained it is an iOS + macOS app.

It doesn't seem to compile on pure macOS project either :thinking:. And I don't see any difference between View declarations in either platform. Looks like a bug, maybe you can file a bug report (maybe on https://feedbackassistant.apple.com/ since this is an Xcode bug?).

Thanks, just not sure if it is an Xcode or Swift 5.3 on macOS bug, but I will file a bug and add the feedback report ID here.

Thanks for your help :).

1 Like

Sorry forgot to attach the feedback assistant bug report ID: FB8800652 (ViewBuilder is not inferred for a SwiftUI's body property for a macOS Target)

@Lantua since you replied you might be interested too, Xcode 12.2 Beta 3 fixed this issue :).

1 Like