Why does break statement not run in switch's default case?

When i try to run break statement in default case, it doesn't run and shows 'Closure containing control flow statement cannot be used with result builder 'ViewBuilder'' error.Why it doesn't run and how to solve this trouble?

Answered by DTS Engineer in 796708022

Hello @Frknakts,

In a result builder, all branches of a switch statement need to produce a value of the type that the result builder expects. In this case, it is a SwiftUI ViewBuilder, and so each branch of the switch must produce a View.

Instead of breaking in the default case, provide a default View value, for example, Text("My Default View").

For greater detail about using switch statements within a result builder, see https://github.com/swiftlang/swift-evolution/blob/main/proposals/0289-result-builders.md.

Best regards,

Greg

Accepted Answer

Hello @Frknakts,

In a result builder, all branches of a switch statement need to produce a value of the type that the result builder expects. In this case, it is a SwiftUI ViewBuilder, and so each branch of the switch must produce a View.

Instead of breaking in the default case, provide a default View value, for example, Text("My Default View").

For greater detail about using switch statements within a result builder, see https://github.com/swiftlang/swift-evolution/blob/main/proposals/0289-result-builders.md.

Best regards,

Greg

In such a case, I just call EmptyView() in the default statement.

Why does break statement not run in switch's default case?
 
 
Q