I am currently trying to learn and advance my skills more as a Swift developer, and this may come across as a dumb question but I'm curious.
Problem
In my code I am constantly repeating UIAlertController creation and presentation code so much that it looks sloppy. Also with Dispatching it to the main thread, it takes up to 5 lines, and I repeat this code throughout my project multiple times, on multiple View Controllers. So instead I have created a "Utilities" class and in that class I have a function that displays a UIAlertController.
Question
What I was wondering is, is this bad coding practice? Is it sloppy to constantly call on this function from another class, creating a new UIAlertController constantly? Does this slow my application done? Or is this perfectly fine?
Code incase it matters:
class Utils {
func displayError(viewController: UIViewController, title: String, message: String, action: UIAlertAction?) {
let ac = UIAlertController(title: title,
message: message,
preferredStyle: .alert)
if action == nil {
ac.addAction(UIAlertAction(title: "Ok", style: .cancel))
} else {
ac.addAction(action!)
}
DispatchQueue.main.async {
viewController.present(ac, animated: true)
}
}
}
Thank you in advanced.