---
title: "alert(item:content:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/alert(item:content:)"
---

# alert(item:content:)

Presents an alert to the user.

## Declaration

```swift
nonisolated func alert<Item>(item: Binding<Item?>, content: (Item) -> Alert) -> some View where Item : Identifiable

```

## Parameters

- `item`: A binding to an optional source of truth for the alert. if item is non-nil, the system passes the contents to the modifier’s closure. You use this content to populate the fields of an alert that you create that the system displays to the user. If item changes, the system dismisses the currently displayed alert and replaces it with a new one using the same process.
- `content`: A closure returning the alert to present.

## Discussion

Discussion Use this method when you need to show an alert that contains information from a binding to an optional data source that you provide. The example below shows a custom data source FileInfo whose properties configure the alert’s message field: struct FileInfo: Identifiable {     var id: String { name }     let name: String     let fileType: UTType }

struct ConfirmImportAlert: View {     @State private var alertDetails: FileInfo?     var body: some View {         Button("Show Alert") {             alertDetails = FileInfo(name: "MyImageFile.png",                                     fileType: .png)         }         .alert(item: $alertDetails) { details in             Alert(title: Text("Import Complete"),                   message: Text("""                     Imported \(details.name) \n File                     type: \(details.fileType.description).                     """),                   dismissButton: .default(Text("Dismiss")))         }     } }

## See Also

### View presentation modifiers

- [actionSheet(isPresented:content:)](swiftui/view/actionsheet(ispresented:content:).md)
- [actionSheet(item:content:)](swiftui/view/actionsheet(item:content:).md)
- [alert(isPresented:content:)](swiftui/view/alert(ispresented:content:).md)
