Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zaimramlan/804c9aaf313ed1d1e01859ca43022d0a to your computer and use it in GitHub Desktop.
Save zaimramlan/804c9aaf313ed1d1e01859ca43022d0a to your computer and use it in GitHub Desktop.
[PLAYGROUND] Simplify Storyboard Instantiation
import UIKit
enum Storyboard: String {
case SignUp
case TopUp
}
enum ViewController {
enum SignUp: String {
case createAccount = "CreateAccountVC"
}
enum TopUp: String {
case enterAmount = "EnterAmountVC"
}
}
extension UIStoryboard {
static func instantiate(signUp viewController: ViewController.SignUp, bundle resource: BundleType? = nil) {
print(Storyboard.SignUp.rawValue)
print(viewController.rawValue)
print(fetchBundle(for: resource ?? .base))
}
static func instantiate(topUp viewController: ViewController.TopUp, bundle resource: BundleType? = nil) {
print(Storyboard.TopUp.rawValue)
print(viewController.rawValue)
print(fetchBundle(for: resource ?? .base))
}
private static func fetchBundle(for resource: BundleType) -> Bundle? {
if let path = Bundle.main.path(forResource: resource.rawValue, ofType: "lproj") {
return Bundle(path: bundlePath)
}
return nil
}
enum BundleType {
case base, localized
var rawValue: String {
switch self {
case .base:
return "Base"
case .localized:
// fetch user's chosen language
return "Thai"
}
}
}
}
UIStoryboard.instantiate(topUp: .enterAmount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment