Skip to content

Instantly share code, notes, and snippets.

@thejohnlima
Created March 5, 2022 21:28
Show Gist options
  • Save thejohnlima/f3deead07ad01447d39815591b852f42 to your computer and use it in GitHub Desktop.
Save thejohnlima/f3deead07ad01447d39815591b852f42 to your computer and use it in GitHub Desktop.
Extensions to help on UIStoryboard files configurations
import UIKit
extension UIStoryboard {
// MARK: - Enums
/// All the storyboards files names should be added here.
/// **Important info:** Under the identity inspector on storyboard file, the storyboard id be the class name.
///
/// **Example**
/// - `profile` represents the storyboard file *Profile.storyboard*
/// - `cards` represents the storyboard file *Cards.storyboard*
///
/// **How to use**
/// ```
/// let navigation = UIStoryboard.cards.instantiateInitialViewController()
/// let controller = UIStoryboard.profile.instantiate(ProfileViewController.self)
/// ```
private enum Name: String {
case profile
case cards
private var filename: String {
rawValue.firstCapitalized
}
var storyboard: UIStoryboard {
UIStoryboard(name: filename, bundle: nil)
}
}
// MARK: - Properties
static var profile: UIStoryboard { Name.profile.storyboard }
static var cards: UIStoryboard { Name.cards.storyboard }
// MARK: - Public Actions
func instantiate<T>(_ identifier: T.Type) -> T where T: UIViewController {
let className = String(describing: identifier)
guard let viewController = instantiateViewController(withIdentifier: className) as? T else {
fatalError("❌ Cannot find controller with identifier \(className)")
}
return viewController
}
}
extension String {
var firstCapitalized: String {
guard let first = first else { return "" }
return String(first).uppercased() + dropFirst()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment