title: Declare array of classes that conform to a protocol
body: <p>Lets say I have created this protocol and a couple of classes</p>
  
  <pre><code>import UIKit
  
  protocol ControllerConstructorProtocol {
      class func construct() -&gt; UIViewController?
  }
  
  class MyConstructor: ControllerConstructorProtocol {
      class func construct() -&gt; UIViewController? {
          return UIViewController()
      }
  }
  
  class MyOtherConstructor: ControllerConstructorProtocol {
      class func construct() -&gt; UIViewController? {
          return UITableViewController(style: .Grouped)
      }
  }
  </code></pre>
  
  <p>Now I want to declare an array that contains classes of objects that will conform to such protocol.
  How can I declare it? Ideally I would like the compiler to check the array is correctly filled (at compile time) rather than checking it myself at (run time) with the <code>as</code> operator.</p>
  
  <p>This is what I have tried without success :(</p>
  
  <ol>
  <li><p>This leads to a compile error:  </p>
  
  <blockquote>
    <p>'Any Object does not have a member named 'construct'</p>
  </blockquote>
  
  <pre><code>var array = [
      MyConstructor.self,
      MyOtherConstructor.self,
  ]
  
  var controller = array[0].construct() // &lt;&lt; ERROR here
  </code></pre></li>
  <li><p>Writing this is even worse, since the class itself does not conform
  to the protocol (their instances do)</p>
  
  <blockquote>
    <p>Type 'MyConstructor.Type' does not conform to protocol
    'ControllerConstructorProtocol'  </p>
  </blockquote>
  
  <pre><code>var array: Array&lt;ControllerConstructorProtocol&gt; = [
      MyConstructor.self, // &lt;&lt; ERROR here
      MyOtherConstructor.self,
  ]
  </code></pre></li>
  </ol>
  
  <p><strong>EDIT 2016/04/23</strong>: In Swift 2.2 (Xcode 7.3) it is possible to write <a href="https://stackoverflow.com/a/28691376/149008">@rintaro's original idea</a> :)</p>
  
  <pre><code>let array: Array&lt;ControllerConstructorProtocol.Type&gt; = [
      MyConstructor.self,
      MyOtherConstructor.self,
  ]
  let viewController = array[0].construct()
  </code></pre>