Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chanonly123/f6ffc950e300885586174ea6eaafb2c0 to your computer and use it in GitHub Desktop.
Save chanonly123/f6ffc950e300885586174ea6eaafb2c0 to your computer and use it in GitHub Desktop.
Wrapping up boilerplate code - Interface builder
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