Created
July 14, 2022 19:30
-
-
Save lucasfeijo/4d6a73fc73ff6320b37df7c2744ee0cb to your computer and use it in GitHub Desktop.
SwiftUI easy Error alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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