How to display Alerts with different messages in SwiftUI in a simple and elegant way ?

  • the first and most used using the 'isPresented' binding
.alert(isPresented: Binding<Bool>, content: () -> Alert)
  • the second using the 'item' binding
.alert(item: Binding<Identifiable?>, content: (Identifiable) -> Alert)
struct AlertID: Identifiable {
var id: AlertType
enum AlertType {
case .error1
case .error2
case .error3
@State private var alertId: AlertID?
private func alertBuilder(alertId: AlertID) -> Alert {
switch {
case .error1:
return Alert(title: Text(“Error 1”))
case .error2:
return Alert(title: Text(“Error 2))
case .error3 :
return Alert(title: Text(“Error 3”))
.alert(item: $alertId) {(alertId) -> Alert in
createAlert(alertId: alertId)
self.parent.alertId = AlertID(id: .otherError)



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Valentin Quelquejay

Valentin Quelquejay

🇨🇭Cybersecurity student. DJ & Musician. Interested about too much things. Improving every minute.