Guest User

Untitled

a guest
Jan 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. import UIKit
  2.  
  3. protocol NibInitiable {
  4. static func loadFromNib() -> Self?
  5. static func loadFromNibForced() -> Self
  6. }
  7.  
  8. extension NibInitiable where Self : UIViewController {
  9. static func loadFromNib() -> Self? {
  10. return UIViewController(nibName: String(describing: self), bundle: Bundle.main) as? Self
  11. }
  12.  
  13. static func loadFromNibForced() -> Self {
  14. guard let vc = loadFromNib() else { fatalError("Couldn't load \(String(describing:self)) from nib.") }
  15. return vc
  16. }
  17. }
  18.  
  19. class C2MViewController : UIViewController, NibInitiable { }
  20. class MediumViewController : UIViewController, NibInitiable { }
  21.  
  22. let vc = C2MViewController.loadFromNib() // C2MViewController?
  23. let vcForced = C2MViewController.loadFromNibForced() // C2MViewController
  24.  
  25. let anotherVC = MediumViewController.loadFromNib() // MediumViewController?
  26. let anotherVCForced = MediumViewController.loadFromNibForced() // MediumViewController
Add Comment
Please, Sign In to add comment