NEW: Subscribe to Hacking with Swift+ and accelerate your learning! >>

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.

   

Thanks!

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: https://developer.apple.com/documentation/swift/error (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. https://www.hackingwithswift.com/sixty/2/9/enum-associated-values

   

Hacking with Swift is sponsored by NSSpain

SPONSORED Announcing NSSpain 2020: Remote Edition! An online, continuous conference for iOS developers. We’ll start on Thursday and finish on Friday, with talks, activities, and lots of fun for 36 hours, non-stop. Sound good? Join us!

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

Not logged in

Log in
 

Link copied to your pasteboard.