Is it proper to add an extension to UIViewController class, or is there a preferred way of adding another method to an established Swift Class?

I have an iOS app with multiple subclasses of UIViewControllers. There are many type of UIAlertControllers I might need to use based on user interaction, internet connection, and catching any other fatal errors.

So I wrote the extension for UIViewController below, which works just fine. And I can call from any of my UIViewControllers as simply as:

myErrors(error: MyErrors.e1.rawValue, title: "Internet Error", msg: "Unable to connect to Internet\nTry Again?")

While this works, I do not know if it's proper to add an extension to UIViewController. Is this considered bad practice? Is there another way I should be pursuing this?

extension UIViewController {
    func myErrors(error: MyErrors, title: String, msg: String)
    {
        var title = ""
        var message = ""
        
        switch error {
        case .e1:
            title = String(format: "%@", title)
            message = String(format: "Database Error %03d%@\n", error.rawValue, msg)
        case .e2:
            title = String(format: "%@", title)
            message = String(format: "Internet Error %03d%@\n", error.rawValue, msg)
        case .e3:
            title = String(format: "%@", title)
            message = String(format: "User Error %03d%@\n", error.rawValue, msg)
        }

        let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert)

        switch error {
        case .e1:
            alert.addAction(UIAlertAction(title: "No", style: .init(rawValue: 0)!, handler: { (action: UIAlertAction!) in
                // ..log error
                //...proceed to code based on No ....
            }))
            alert.addAction(UIAlertAction(title: "Yes", style: .init(rawValue: 0)!, handler: { (action: UIAlertAction!) in
                // ..log error
                //...code based on Yes ....
            }))
        case .e2:
                // No user option availabe in this alert, just OK
                // ... do all logging of errors
                // proceed
        case .e3:
                // Add specific acctions to this error
                // ... do all logging of errors
                // proceed
        }
        self.present(alert, animated: true, completion: nil)
    }
}
Answered by robnotyou in 703127022

You are using an Extension to add functionality to an existing class (UIViewController) that you don't "own".

This is exactly what Extension is for.

A fuller explanation:
https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-extensions-in-swift

Accepted Answer

You are using an Extension to add functionality to an existing class (UIViewController) that you don't "own".

This is exactly what Extension is for.

A fuller explanation:
https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-extensions-in-swift

Is it proper to add an extension to UIViewController class, or is there a preferred way of adding another method to an established Swift Class?
 
 
Q