Skip to content

Instantly share code, notes, and snippets.

@kvaDrug
Created February 20, 2019 21:37
Show Gist options
  • Save kvaDrug/e1d88092f38eb38b0c6cd91a6f7be590 to your computer and use it in GitHub Desktop.
Save kvaDrug/e1d88092f38eb38b0c6cd91a6f7be590 to your computer and use it in GitHub Desktop.
A Swift protocol allowing to 1) shorten the code required to instantiate an UIViewController from a storyboard 2) store the storyboard id in the predictable place
import UIKit
/**
This protocol along with the extension provides some syntactic sugar and solves two common problems:
- Shortens the instantiation
- Answers, where to store the storyboard id
Blog post: https://kelindev.blogspot.com/2018/10/uikit-protocol-of-month.html
*/
protocol StoryboardInstantiatableViewController {
static var storyboardId: String { get }
}
extension StoryboardInstantiatableViewController where Self: UIViewController {
static func instantiate(from storyboard: UIStoryboard?) -> Self? {
return storyboard?.instantiateViewController(withIdentifier: Self.storyboardId) as? Self
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment