Skip to content

Instantly share code, notes, and snippets.

@jakubpetrik
Last active February 20, 2019 10:05
Show Gist options
  • Save jakubpetrik/5f7416d0a1ff3ecfe921dd03644887e1 to your computer and use it in GitHub Desktop.
Save jakubpetrik/5f7416d0a1ff3ecfe921dd03644887e1 to your computer and use it in GitHub Desktop.
UIViewController+Instantiable
protocol Instantiable {
static func make() -> Self
}
extension Instantiable where Self: UIViewController {
/// Instantiates controller from storyboard.
/// - example:
/// `let myViewController = MyViewController.make()`
/// - important:
/// Initial controller of the same type must exists in storyboard named as controller's class, otherwise will `fatalError()`.
/// - Returns: Instantiated view controller.
static func make() -> Self {
let storyboard = UIStoryboard(name: String(describing: self), bundle: nil)
guard let instance = storyboard.instantiateInitialViewController() as? Self else { fatalError() }
return instance
}
}
extension UIViewController: Instantiable {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment