Last active
May 2, 2020 07:12
-
-
Save chanonly123/f6ffc950e300885586174ea6eaafb2c0 to your computer and use it in GitHub Desktop.
Wrapping up boilerplate code - Interface builder
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
import UIKit | |
extension UIViewController { | |
// must override to provide storyboard | |
@objc class var fromStoryboard: UIStoryboard? { return nil } | |
// optional override to provide storyboardId | |
@objc class var storyboardId: String { return String(describing: self) } | |
static func instantiate() -> Self { | |
let id = storyboardId | |
if let fromStoryboard = fromStoryboard { | |
let viewController = fromStoryboard.instantiateViewController(withIdentifier: id) | |
if let casted = viewController as? Self { | |
return casted | |
} else { | |
assertionFailure("cannot cast to \(String(describing: self))") | |
} | |
} else { | |
assertionFailure("fromStoryboard is not defined for \(String(describing: self))") | |
} | |
return Self() // wont come here | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment