Apologies for the really basic question, but I am wondering if I'm on the right track.
I'm putting together a small app that downloads JSON data and displays various bits of information in different views. The app has no purpose apart from learning by doing.
One challenge is to give feedback to the user (well, myself :) ) if an error occurs by display an alert box. As there are numerous occasions where things can go wrong, I thought about putting the UIAlertController in a function and using an enum as the function parameter to display the relevant error message.
Is this the most logical way of doing it?
Is there a better way of doing it? It looks a bit pedestrian to me :-)
// MARK: - User Alerts
enum UserAlert: String {
case usernameMissingTitle = "Username Missing"
case usernameMissingMessage = "Please enter a Username"
case noUserFoundTitle = "No Users found"
case noUserFoundMessage = "No user with this nickname could be found"
}
func displayAlert(title: UserAlert.RawValue, message: UserAlert.RawValue){
// Making sure it's running on the main thread if it gets called from a background thread
DispatchQueue.main.async {
// create the alert
let alert = UIAlertController(title: String.init(describing: title), message: String.init(describing: message), preferredStyle: UIAlertController.Style.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}