Last active
December 6, 2018 18:24
-
-
Save alexbaramilis/2d272b031e9eabb07c9b955fb4dc8b2d to your computer and use it in GitHub Desktop.
Dependency Injection with Storyboard
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
@UIApplicationMain | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
var window: UIWindow? | |
func applicationDidFinishLaunching(_ application: UIApplication) { | |
if let navigationController = window?.rootViewController as? UINavigationController, | |
let breatherViewController = navigationController.topViewController as? BreatherViewController { | |
// Property and initialiser injection in one line | |
breatherViewController.dataSource = BreatherDataSource(with: NetworkClient()) | |
} | |
} | |
} | |
// needs property injection | |
class BreatherViewController: UIViewController { | |
// Dependencies | |
var dataSource: BreatherDataSource? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
print(dataSource) // spoiler: not nil! | |
} | |
} | |
// needs initialiser injection | |
class BreatherDataSource { | |
// Dependencies | |
private let networkClient: NetworkClient? | |
init(with networkClient: NetworkClient) { | |
self.networkClient = networkClient | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment