title: Whats the Swift best practice for reusable UIAlertController configuration via Enum
body: <p>I'm trying to make my life a little bit easier and my app more maintainable while at the same time reducing the amount of duplicated code. So I thought it would be nice to put the some code to display a certain type of UIAlertController into its own class.</p>
  
  <p>The thing is, that I have basically the same alert which only differs really slightly based on where I display it in my app. So I thought to myself: why not use a enum and every time I want to display an alert of that kind, just give it the enum value. Get rid of all the duplicated strings everywhere in my code. </p>
  
  <p>I love enums in Swift. They are just so schwifty - ehh I mean swifty. So I came up with this example enum:</p>
  
  <pre><code>enum MyAlertType {
      case a
      case b
  }
  </code></pre>
  
  <p>Next I created a simple class to handle the display:</p>
  
  <pre><code>class MyAlert {
      static func showAlert(ofType type: MyAlertType, inViewController viewController: UIViewController, handler: ((UIAlertAction) -&gt; ())? = nil, completion: (() -&gt; ())? = nil) {
  
          var message: String
          switch type {
          case .a:
              message = "A is a nice letter!"
          case .b:
              message = "B is a nice letter!"
          }
  
          let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
          alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
          viewController.present(alert, animated: true, completion: completion)
      }
  }
  
  //Somewhere in my code (obviously a UIViewController)
  MyAlert.showAlert(ofType: .a), inViewController: self)
  </code></pre>
  
  <p>But wait, I still have to give this function the ViewController where I want to display the alert in. For me thats always the same (self) so the next logical step was to make this an extension:</p>
  
  <pre><code>extension UIViewController {
      func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -&gt; ())? = nil, completion: (() -&gt; ())? = nil) {
  
          var message: String
          switch type {
          case .a:
              message = "A is a nice letter!"
          case .b:
              message = "B is a nice letter!"
          }
  
          let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
          alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
          present(alert, animated: true, completion: completion)
      }
  }
  
  //Somewhere in my UIViewController)
  showAlert(ofType: .a)
  </code></pre>
  
  <p>But this makes the code available for all UIViewControllers, even those where I don't explicitly need/want to display that kind of alert. I mean, yeah sure, I am the developer and I can decide not to use this but on the other hand isn't it always a good practice to hide everything as much as possible and as least as necessary? Who knows who might in the future join my dev team and start misusing my beautiful code in ways I haven't thought of?</p>
  
  <p>The other day I learned about Swifts protocol oriented programming  approach (which to be honest I still have not fully understood by now) and now I think, I maybe should make this a protocol with a default implementation and then let the only those UIViewControllers implement the protocol, where I need to show this alert.</p>
  
  <pre><code>protocol MyAlertProtocol {
      func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -&gt; ())?, completion: (() -&gt; ())?)
  }
  
  extension MyAlertProtocol where Self : UIViewController {
      func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -&gt; ())? = nil, completion: (() -&gt; ())? = nil) {
  
          var message: String
          switch type {
          case .a:
              message = "A is a nice letter!"
          case .b:
              message = "B is a nice letter!"
          }
  
          let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
          alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
          present(alert, animated: true, completion: completion)
      }
  }
  
  extension MyViewController: MyAlertProtocol {}
  </code></pre>
  
  <p>I know that this might sound as an opinion based question so I'm not asking you if you <em>think</em> if this or that is better but to tell me if there actually <strong>is</strong> a best practice for this scenario and how it looks like.</p>
  
  <p>Protocol with default implementation via protocol extension? Simple UIViewController extension? Custom Enum, Struct or Class with static function (and if so, which one)? Or maybe even just a function somewhere in a Swift file? I feel overwhelmed. The agony of choice...</p>
  
  <p><strong>UPDATE/SOLUTION</strong></p>
  
  <p>After reading the given answers, I decided that my Sergeys answer was indeed the most suitable one. I wanted to make my like "easier" by having "less lines of duplicated code". For me this included the "presend(controller:animated:)" inside my ViewController.</p>
  
  <p>However, I think you guys are right. I should use a struct (a class is really not necessary for one static function) with a static func to generate the alert man make it "ready to use" but still let the caller decide where to present it. </p>
  
  <p>By doing it like this, I could use my alert-generation-struct anywhere I want and for example let a delegate present it or pass it around until I reach a UIViewController who can present it, if my caller isn't one.</p>
  
  <p>Therefore in my very simple case, I'll go with:</p>
  
  <pre><code>enum MyAlertType {
      case a
      case b
  }
  
  struct MyAlert {
      static func showAlert(ofType type: MyAlertType, handler: ((UIAlertAction) -&gt; ())? = nil) -&gt; UIAlertController {
  
          var message: String
          switch type {
          case .a:
              message = "A is a nice letter!"
          case .b:
              message = "B is a nice letter!"
          }
  
          let alert = UIAlertController(title: "Do you know which letter is nice?", message: message, preferredStyle: .alert)
          alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: handler))
  
          return alert
      }
  }
  </code></pre>
  
  <p>Thanks to everybody participating in dissolving the blockade in my head. </p>