Contents

buildOptional(_:)

Produces an optional widget for conditional statements in multi-statement closures that’s only visible when the condition evaluates to true.

Declaration

static func buildOptional(_ widget: (any Widget & _LimitedAvailabilityWidgetMarker)?) -> some Widget

Discussion

Conditional statements in a WidgetBundleBuilder can contain an if statement but not an else statement, and the condition can only perform a compiler check for availability, like in the following code:

var body: some Widget {
    if #available(iOS 16, *) {
        WindowGroup {
            ContentView()
        }
    }
}

See Also

Bundling widgets