<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/alert(error:actions:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE5alert5error7actionsQrAA7BindingVyqd__SgG_qd_0_yXEt10Foundation14LocalizedErrorRd__AaBRd_0_r0_lF::OverloadGroup"
  },
  "title" : "alert(error:actions:)"
}
-->

# alert(error:actions:)

Presents an alert when an error is present.

```
@export(implementation) nonisolated func alert<E, A>(error: Binding<E?>, @ContentBuilder actions: () -> A) -> some View where E : LocalizedError, A : View
```

## Parameters

`error`

A binding to an optional localized Error. The system presents
the alert when the binding’s value is non-nil, and uses the error to
generate the alert’s title. When the user presses or taps one of the
alert’s actions, the system sets this value to `nil` and dismisses.
The error’s `errorDescription` will be used as the title.

`actions`

A [`ContentBuilder`](/documentation/SwiftUI/ContentBuilder) returning the alert’s actions.

## Discussion

In the example below, a form conditionally presents an alert depending
upon the value of an error. When the error value isn’t `nil`, the system
presents an alert with an “OK” action.

The title of the alert is inferred from the error’s `errorDescription`.

```
struct TicketPurchase: View {
    @State private var error: TicketPurchaseError? = nil

    var body: some View {
        TicketForm(error: $error)
            .alert(error: $error) {
                Button("OK") {
                    // Handle acknowledgement.
                }
            }
    }
}
```

All actions in an alert dismiss the alert after the action runs.
The default button is shown with greater prominence. You can
influence the default button by assigning it the
[`defaultAction`](/documentation/SwiftUI/KeyboardShortcut/defaultAction) keyboard shortcut.

The system may reorder the buttons based on their role and prominence.

If no actions are present, the system includes a standard “OK”
action. No default cancel action is provided. If you want to show a
cancel action, use a button with a role of [`cancel`](/documentation/SwiftUI/ButtonRole/cancel).

On iOS, tvOS, and watchOS, alerts only support controls with labels that
are [`Text`](/documentation/SwiftUI/Text). Passing any other type of view results in the content
being omitted.

This modifier creates a [`Text`](/documentation/SwiftUI/Text) view for the title on your behalf, and
treats the localized key similar to
[`init(_:tableName:bundle:comment:)`](/documentation/SwiftUI/Text/init(_:tableName:bundle:comment:)). See [`Text`](/documentation/SwiftUI/Text) for more
information about localizing strings.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)