Last active
November 20, 2019 04:04
-
-
Save karthikgs7/50c27486cb0e4db80e58702ad2f07cc2 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import Foundation | |
import UIKit | |
extension UIStoryboard { | |
enum Storyboard { | |
case main | |
case order | |
var name: String { | |
switch self { | |
case .main: | |
return "Main" | |
case .order: | |
return "Order" | |
} | |
} | |
} | |
convenience init(storyboard: Storyboard, bundle: Bundle? = nil) { | |
self.init(name: storyboard.rawValue, bundle: bundle) | |
} | |
} | |
protocol StoryboardIdentifiable { | |
static var storyboardIdentifier: String { get } | |
} | |
extension StoryboardIdentifiable where Self: UIViewController { | |
static var storyboardIdentifier: String { | |
return String(describing: self) | |
} | |
} | |
extension UIViewController: StoryboardIdentifiable { } | |
extension UIStoryboard { | |
func instantiateViewController<T: UIViewController>() -> T { | |
let optionalViewController = self.instantiateViewController(withIdentifier: T.storyboardIdentifier) | |
guard let viewController = optionalViewController as? T else { | |
fatalError("Couldn't instantiate view controller with identifier \(T.storyboardIdentifier)") | |
} | |
return viewController | |
} | |
func instantiateViewController<T: UIViewController>(_: T.Type) -> T { | |
let optionalViewController = self.instantiateViewController(withIdentifier: T.storyboardIdentifier) | |
guard let viewController = optionalViewController as? T else { | |
fatalError("Couldn't instantiate view controller with identifier \(T.storyboardIdentifier)") | |
} | |
return viewController | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment