Last active
February 20, 2019 10:05
-
-
Save jakubpetrik/5f7416d0a1ff3ecfe921dd03644887e1 to your computer and use it in GitHub Desktop.
UIViewController+Instantiable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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