Help with ViewBuilders and SwiftUI (Xcode 12, Swift 5.3)

Hello Everyone,

Posted it originally in the Using Swift section here:
https://forums.swift.org/t/help-viewbuilder-and-swiftui-issue-swift-5-3/40862

A colleague and I are trying to refactor some code to take advantage of new features in Swift 5.3 and more specifically of the view builder changes that would allow us to remove some of the AnyView casting we were doing.

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 should be treated as a special case and inherit the ViewBuilder was inferred by the compiler ( https://developer.apple.com/wwdc20/10170 1 ): <you can see the pictures in the Swift.org forum thread above>.

Do you see a silly mistake in there?

Thank you in advance for your help!

Kind Regards,

Goffredo


Accepted Reply

An update from the quoted thread above:

Example:

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


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): https://www.icloud.com/iclouddrive/01bKAFXVd82HpGZ-3R7RExKeA#ViewBuilder_iOS_macOS
Not sure why it would not work for macOS, there could be the embarrassing thing I was missing and I should have explained it is an iOS + macOS app.

So it works as a pure iOS project, but not as a pure macOS project. Will be creating a Feedback report and post the ID here.


Replies

An update from the quoted thread above:

Example:

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


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): https://www.icloud.com/iclouddrive/01bKAFXVd82HpGZ-3R7RExKeA#ViewBuilder_iOS_macOS
Not sure why it would not work for macOS, there could be the embarrassing thing I was missing and I should have explained it is an iOS + macOS app.

So it works as a pure iOS project, but not as a pure macOS project. Will be creating a Feedback report and post the ID here.


FB8800652 (ViewBuilder is not inferred for a SwiftUI's body property for a macOS Target)

Tapped the “Answered” icon by mistake and I cannot undo it :/...
Fixed in Xcode 12.2 Beta 3