<!--
{
  "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(isPresented:error:actions:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE5alert11isPresented5error7actionsQrAA7BindingVySbG_qd__Sgqd_0_yXEt10Foundation14LocalizedErrorRd__AaBRd_0_r0_lF"
  },
  "title" : "alert(isPresented:error:actions:)"
}
-->

# alert(isPresented:error:actions:)

Presents an alert when an error is present.

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

## Parameters

`isPresented`

A binding to a Boolean value that determines whether to
present the alert. When the user presses or taps one of the alert’s
actions, the system sets this value to `false` and dismisses.

`error`

An optional localized Error that is used to generate the
alert’s title.  The system passes the contents to the modifier’s
closures. You use this data to populate the fields of an alert that
you create that the system displays to the user.

`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
    @State private var showAlert = false

    var body: some View {
        TicketForm(showAlert: $showAlert, error: $error)
            .alert(isPresented: $showAlert, 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)