title: UIViewController extension to instantiate from storyboard
body: <p>I'm trying to write a little extension in Swift to handle instantiation of a <code>UIViewController</code> from a storyboard.</p>
  <p>My idea is the following: Since <code>UIStoryboard</code>'s method <code>instantiateViewControllerWithIdentifier</code> needs an identifier to instantiate a given storyboard's view controller, why don't assign every view controller in my storyboard an identifier equal to its exact class name (i.e a <code>UserDetailViewController</code> would have an identifier of &quot;UserDetailViewController&quot;), and, create a class method on UIViewController that would:</p>
  <ul>
  <li>accept a <code>UIStoryboard</code> instance as a unique parameter</li>
  <li>get the current class name as a string</li>
  <li>call <code>instantiateViewControllerWithIdentifier</code> on the storyboard instance with the class name as a parameter</li>
  <li>get the newly created <code>UIViewController</code> instance, and return it</li>
  </ul>
  <p>So, instead of (which repeats the class name as a string, not very nice)</p>
  <pre><code>let vc = self.storyboard?.instantiateViewControllerWithIdentifier(&quot;UserDetailViewController&quot;) as UserDetailViewController
  </code></pre>
  <p>it would be:</p>
  <pre><code>let vc = UserDetailViewController.instantiateFromStoryboard(self.storyboard!)
  </code></pre>
  <p>I used to do it in Objective-C with the following category:</p>
  <pre><code>+ (instancetype)instantiateFromStoryboard:(UIStoryboard *)storyboard
  {
      return [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([self class])];
  }
  </code></pre>
  <p>But I'm completely stuck with the Swift version. I hope is that there is some kind of way to do it.
  I tried the following:</p>
  <pre><code>extension UIViewController {
      class func instantiateFromStoryboard(storyboard: UIStoryboard) -&gt; Self {
          return storyboard.instantiateViewControllerWithIdentifier(NSStringFromClass(Self))
      }
  }
  </code></pre>
  <p>Returning <code>Self</code> instead of <code>AnyObject</code> allows the type inference to work. Otherwise, I would have to cast every single return of this method, which is annoying, but maybe you have a better solution?</p>
  <p>This gives me the error: <code>Use of unresolved identifier 'Self'</code>
  The <code>NSStringFromClass</code> part seems to be the problem.</p>
  <p>What do you think?</p>
  <ul>
  <li><p>Is there any way to return <code>Self</code> from class functions?</p>
  </li>
  <li><p>How would you get this working without the need to cast the return value every time? (i.e keeping <code>-&gt; Self</code> as return value)</p>
  </li>
  </ul>