Skip to content

Instantly share code, notes, and snippets.

@alexbaramilis
Last active December 6, 2018 18:24
Show Gist options
  • Save alexbaramilis/2d272b031e9eabb07c9b955fb4dc8b2d to your computer and use it in GitHub Desktop.
Save alexbaramilis/2d272b031e9eabb07c9b955fb4dc8b2d to your computer and use it in GitHub Desktop.
Dependency Injection with Storyboard
@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