Skip to content

Instantly share code, notes, and snippets.

@lucasfeijo
Created July 14, 2022 19:30
Show Gist options
  • Save lucasfeijo/4d6a73fc73ff6320b37df7c2744ee0cb to your computer and use it in GitHub Desktop.
Save lucasfeijo/4d6a73fc73ff6320b37df7c2744ee0cb to your computer and use it in GitHub Desktop.
SwiftUI easy Error alert
public extension View {
func alert(title: String? = "Oops", error: Binding<Error?>) -> some View {
alert(isPresented: Binding<Bool>.init(get: {
error.wrappedValue != nil
}, set: { setVal in
if !setVal {
error.wrappedValue = nil
}
})) {
Alert(title: Text(title), message: Text(error.wrappedValue?.localizedDescription ?? ""), dismissButton: .cancel())
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment