NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

Babysteps with functions and alert messages

Forums > Swift

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)


I think the idea is fine but I would not mix titles and messages in a single enum because ideally all enum cases should represent one thing but half of them represent title and the other half represent message. You can also use rawValue property on enum to avoid the String.init

Although in this particular case I would look into creating my own Error to encapsulate the problem and your displayAlert function can then decide how to present particular error.



I did have 2 enums to start with but thought it would be neater to compress it into one as it would 'all be in one place', but yeah, the way it's done here may not be ideal :-)

Fair point on rawvalue, much easier this way.

I assume I have this to read and understand now: (which is probably the non-pedestrian way of achieving the same result).

thanks again, much appreciated.


Yes that is a good place to start. Basically the only requirements are to adopt the Error protocol and then you can use your own type with try catch keywords and use throw to "throw it".

Enum is probably the best option for simple errors, and you can even use associated values to provide additional info.


Hacking with Swift is sponsored by Fernando Olivares

SPONSORED Would you describe yourself as knowledgeable, but struggling when you have to come up with your own code? Fernando Olivares has a new book containing iOS rules you can immediately apply to your coding habits to see dramatic improvements, while also teaching applied programming fundamentals seen in refactored code from published apps.

Try the book!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.