Skip to content

Instantly share code, notes, and snippets.

@moaible
Last active November 13, 2017 23:26
Show Gist options
  • Select an option

  • Save moaible/fb5a56519ed5999d36785a166503a1ef to your computer and use it in GitHub Desktop.

Select an option

Save moaible/fb5a56519ed5999d36785a166503a1ef to your computer and use it in GitHub Desktop.
StoryboardInstantiate.swift
import UIKit
protocol StoryboardInstantiate {
static var storyboardIdentifier: String { get }
static var storyboardName: String { get }
}
extension UIStoryboard {
static func instantiateViewController<ViewController: UIViewController>() -> ViewController where ViewController: StoryboardInstantiate {
let storyboard = UIStoryboard(name: ViewController.storyboardName, bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: ViewController.storyboardIdentifier)
guard let instantiatedViewController = viewController as? ViewController else {
fatalError("Unmatche ViewController class: \(type(of: viewController))")
}
return instantiatedViewController
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment