How to Implement Dependency Injection In iOS [Swift 5, iOS 12, Step-by-Step]

Поділитися
Вставка
  • Опубліковано 31 гру 2024

КОМЕНТАРІ • 16

  • @AryamanSharda
    @AryamanSharda  3 роки тому +6

    Sorry if I sounded a little under the weather, I was recovering from a spirited July 4th weekend...

  • @Khan_O
    @Khan_O 2 роки тому

    Oh my god beautiful. I found an answer to an entirely different thing i was trying to figure out for days. Subscribed for life.

    • @AryamanSharda
      @AryamanSharda  2 роки тому

      Glad you found it helpful! Out of curiosity, what was the problem you were looking into?

  • @eyupmert7338
    @eyupmert7338 9 місяців тому

    Great video. Thanks for this informative content ✌

  • @MrAksp
    @MrAksp 2 роки тому

    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

  • @akashrao11
    @akashrao11 3 роки тому +1

    great video - you should have explained "how to do DI if we load viewcontroller from storyboard"

    • @PavelPalancica
      @PavelPalancica 11 місяців тому

      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)
      }
      }

  • @rungxanh2901
    @rungxanh2901 2 роки тому

    This is very visual. Thank you very much 🙏

  • @TheSpiralnotizblock
    @TheSpiralnotizblock 2 роки тому

    Did you released the coordinator pattern video? Havent found it.

  • @philipplamenov8649
    @philipplamenov8649 3 роки тому

    Verry helpful video! Thank you.

  • @waheedafolabi6929
    @waheedafolabi6929 3 роки тому

    Great concise content as always…thanks!

  • @thunderzz2233
    @thunderzz2233 2 роки тому +2

    Is the coordinator pattern video released yet?

    • @AryamanSharda
      @AryamanSharda  2 роки тому +2

      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!

    • @thunderzz2233
      @thunderzz2233 2 роки тому

      @@AryamanSharda Thank you so much!!

    • @eugenest1137
      @eugenest1137 2 роки тому

      @@AryamanSharda Hi bro, thank you for the great content. Waiting for MVVM-C, thank you ;)

  • @amrhesham6720
    @amrhesham6720 3 роки тому

    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 :)