Wow, explained a lot in short time. It answer my lot of other questions also, and for every interview, I am going to use this ref. Thanks a lot Aryaman
There are 2 options: 1) Use the new API for Initializer Injection (introduced in iOS 13): --- FeedViewController.swift --- final class FeedViewController: UIViewController { private var loader: ImageLoader // ... init?(coder: NSCoder, loader: ImageLoader) { self.loader = loader super.init(coder: coder) } required init?(coder: NSCoder) { fatalError("Use `init(coder:loader:)` to initialize a `FeedViewController` instance.") } } --- MainAppCoordinator.swift --- final class MainAppCoordinator: Coordinator { private var loader: FeedLoader // ... func showFeed() { let feedVC = UIStoryboard(name: "Main", bundle: .main) .instantiateViewController(identifier: "FeedVC", creator: { coder -> FeedViewController? in FeedViewController(coder: coder, feedLoader: feedLoader) }) present(feedVC, animated: true) } } 2) Simply load the VC from the Storyboard file, and then use Property Injection. Will have to declare the properties as Optionals (otherwise the compiler will complain). In either case, it's probably better to have a VCs Factories class, that would take care of VCs cration as well as injecting the necessary dependencies. You will just call those methods like this: --- VCsFactory.swift --- final class VCsFactory { private static var loader = FeedLoader() // ... static func createFeedVC() { let feedVC = // Initialize using one of the 2 ways feedVC.loader = loader return feedVC } } --- MainAppCoordinator.swift --- final class MainAppCoordinator: Coordinator { // ... func showFeed() { let feedVC = VCsFactory.createFeedVC() present(feedVC, animated: true) } }
Great content Bro... but i think it would be even better if you depend on protocols instead of concrete types.. if you want someone kind of partner in your upcoming videos let us have some chat :)
Sorry if I sounded a little under the weather, I was recovering from a spirited July 4th weekend...
Oh my god beautiful. I found an answer to an entirely different thing i was trying to figure out for days. Subscribed for life.
Glad you found it helpful! Out of curiosity, what was the problem you were looking into?
Great video. Thanks for this informative content ✌
Wow, explained a lot in short time. It answer my lot of other questions also, and for every interview, I am going to use this ref. Thanks a lot Aryaman
great video - you should have explained "how to do DI if we load viewcontroller from storyboard"
There are 2 options:
1) Use the new API for Initializer Injection (introduced in iOS 13):
--- FeedViewController.swift ---
final class FeedViewController: UIViewController {
private var loader: ImageLoader
// ...
init?(coder: NSCoder, loader: ImageLoader) {
self.loader = loader
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError("Use `init(coder:loader:)` to initialize a `FeedViewController` instance.")
}
}
--- MainAppCoordinator.swift ---
final class MainAppCoordinator: Coordinator {
private var loader: FeedLoader
// ...
func showFeed() {
let feedVC = UIStoryboard(name: "Main", bundle: .main)
.instantiateViewController(identifier: "FeedVC",
creator: { coder -> FeedViewController? in
FeedViewController(coder: coder, feedLoader: feedLoader)
})
present(feedVC, animated: true)
}
}
2) Simply load the VC from the Storyboard file, and then use Property Injection. Will have to declare the properties as Optionals (otherwise the compiler will complain).
In either case, it's probably better to have a VCs Factories class, that would take care of VCs cration as well as injecting the necessary dependencies. You will just call those methods like this:
--- VCsFactory.swift ---
final class VCsFactory {
private static var loader = FeedLoader()
// ...
static func createFeedVC() {
let feedVC = // Initialize using one of the 2 ways
feedVC.loader = loader
return feedVC
}
}
--- MainAppCoordinator.swift ---
final class MainAppCoordinator: Coordinator {
// ...
func showFeed() {
let feedVC = VCsFactory.createFeedVC()
present(feedVC, animated: true)
}
}
This is very visual. Thank you very much 🙏
Did you released the coordinator pattern video? Havent found it.
Verry helpful video! Thank you.
Great concise content as always…thanks!
Is the coordinator pattern video released yet?
I’ve taken a short hiatus from UA-cam to focus on writing a book about interviewing for iOS roles. I’ll get back to video making next week!
@@AryamanSharda Thank you so much!!
@@AryamanSharda Hi bro, thank you for the great content. Waiting for MVVM-C, thank you ;)
Great content Bro... but i think it would be even better if you depend on protocols instead of concrete types.. if you want someone kind of partner in your upcoming videos let us have some chat :)