Created
July 10, 2019 15:01
-
-
Save sho-ito-1027/3433e2e1c635467b524fc7258a777e67 to your computer and use it in GitHub Desktop.
Storyboardを使ったDIもどき
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
final class ViewController: UIViewController { | |
// MARK: - Private Property | |
private var dependency: Int! | |
// MARK: - Initilizer | |
static func initiate(dependency: Int) -> ViewController { | |
let viewController = UIStoryboard.instantiateInitialViewController(from: self) | |
viewController.dependency = dependency | |
return viewController | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
} | |
extension UIStoryboard { | |
public static func instantiateInitialViewController<T>(from className: T.Type) -> T where T : UIViewController { | |
let name = String(describing: className.self) | |
guard let viewController = UIStoryboard(name: name, bundle: .main).instantiateInitialViewController() as? T else { | |
fatalError("型が不一致") | |
} | |
return viewController | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment