Swinject: Effortless Dependency Injection in iOS (Basics)

  Переглядів 27,931

iOS Academy

iOS Academy

2 роки тому

In this video we will explore the basics of dependency injection in iOS with a popular framework called Swinject. We'll walk through a practical example of using swinject to bridge various interface driven dependencies.
github.com/Swinject/Swinject
💻 Source Code: / iosacademy
🎥 Subscribe for more: ukposts.info?su...
😎 Like my teaching style? Check out some of my most popular courses! courses.iosacademy.io
👉🏼 Connect (personal LinkedIn) / afraz-siddiqui
🚀 Follow on LinkedIn / ios-academy
** Popular Series
Building Instagram: courses.iosacademy.io/p/build...
Building TikTok: / @iosacademy
SwiftUI for Beginners: ios-academy.teachable.com/p/s...
** Get Skillshare free for 2 Months and learn iOS
www.skillshare.com/r/user/afraz
** Manage all your investments from app earnings on Betterment!
bit.ly/3eBwlI9
** Grow your own UKposts tech channel with TubeBuddy:
www.tubebuddy.com/iosacademy
#swift #swinject #dependencyInjection

КОМЕНТАРІ: 60
@NicolasFrugoni1
@NicolasFrugoni1 2 роки тому
I guess this may be just for keeping the example simple enough. But there are a couple of bad practices here that violates a couple of principles. just for the record of someone reading comments and wanting to go into more details, I'll list out a few things to improve. * Making a dependency optional just because the framework works that way is not good, not only makes you type defensive code (like setting a default value if the dependency is not injected correclty) but also expreses an incorrect fact about the class. receiving an optional value means that the class will be able to work without that value. * Referencing and configuring the container from something other than the root composition layer is a bad/incomplete implementation of the solid principles - S: Your ViewController class is not only responsible for the UI, but for creating the next instance of the SecondVC. - O: You can't change to what VC you'll be navigating without modifying the ViewControllers' class code. - L and ID: Since ViewController is depending on the container, configuring it and instantiating the next VC, (described above) this makes imposible to swap this functionality with a different implementation or even aggregate or compose more functionality. View Controller should have received on it's constructor an abstraction of what's needed to be done, or what happened, and the implementation of that should have been injected (an abstraction can be a Protocol, but a function as well!) simplified example: ``` ViewController { init(onButtonTapped: (() -> Void)?) func didTapButton() { onButtonTapped?() } } ``` Then whoever creates the ViewController is in charge of configuring the Container and creating the Second VC as well the responsable for that, is the root composition layer This layer lives as close to the app's start point as possible, because is app specific. simplified example: ``` SceneDelegate { func loadSceneForWindow() { let container = Container() // configure container and register dependencies ViewController(onButtonTapped: { container.resolve(SecondVC)... } ) } } ``` This may be a bit more advanced, but it's the complete story on how and why implementing the SOLID principles lead to more composable and maintainable code. as you may see, if you move everything that's composition related to the composition root, the need for a Container is rather debatable, since the code for managing instances is extremely simple
@MrTalhakhan01
@MrTalhakhan01 2 роки тому
I believe there will be a potential / need to use this way. But as a mid-senior dev, I didnt fully understand the use case of this complex implementation
@dayapangestu9218
@dayapangestu9218 2 роки тому
one such use case is testing, by passing object in constructor, you can change implementation of protocol to either actual object or mock object, lets say on SecondVC constructor you need network repository that call method getCityArray , in actual implementation you pass the actual object of network repository that handle API call to call getCityArray, but this implementation would be costly if you use CI/CD on every test, alternatively you can use a mock network repository that return immediately a fake array of city
@yefimchukv
@yefimchukv 2 роки тому
WOW! That's what I needed so much after entering a project with swinject. Also would be nice to see a video about Kingfisher
@iOSAcademy
@iOSAcademy 2 роки тому
You got it!
@ahmetbostanckloglu8829
@ahmetbostanckloglu8829 2 роки тому
I was waiting this kind of topic. For example Solid Principle and dependency injection. Thank you for making this video.
@iOSAcademy
@iOSAcademy 2 роки тому
Youre welcome
@JP-ks3kh
@JP-ks3kh 2 роки тому
You the best! Thanks for the great tutorial.
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks
@Blobiverse
@Blobiverse 2 роки тому
Thank you, exactly what I needed right now 😊 (also seconded on video on Kingfisher)
@iOSAcademy
@iOSAcademy 2 роки тому
Coming soon!
@basoahmad7516
@basoahmad7516 2 роки тому
Great tutorial as always!
@iOSAcademy
@iOSAcademy 2 роки тому
Thank you! Cheers!
@ofgokce
@ofgokce 2 роки тому
I have recently gotten involved with DI on the project I'm working on. My senior told me to do a research about this exact framework and I did. But I was expecting it to be more Swifty. After a month of loose working on the subject I have developed my own DI framework which was working via the property wrappers for syntactic sugary (e.g. injecting the dependencies with @Injected keyword as properties and some more features). I don't know if there is some deep architectural programming with Swinject but from your lovely video I must say that it is what I thought it is. Except maybe the part where you pointed out that the dependencies aren't initiated before calling. But for my use case I needed to replace some singletons which ought to work globally and hold data globally and be same everywhere and make them testable. If there is more to Swinject that I didn't catch yet or some advanced features of it, if you have the time I would love to see your take on it. Please keep up the good work mate and thank you for sharing your wisdom with us. Cheers!
@andronisaev
@andronisaev 2 роки тому
Great video for beginners! 👏🏻👏🏻
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks
@trungnghia2938
@trungnghia2938 2 роки тому
Thanks so much, this is great
@iOSAcademy
@iOSAcademy 2 роки тому
Youre welcome
@robinandthedog
@robinandthedog 2 роки тому
Top video! thanks.
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks
@vinitgoyani5664
@vinitgoyani5664 Рік тому
How did you direct add that button code can you explain it?
@nigelpstreet
@nigelpstreet 2 роки тому
Intended as constructive feedback: Your tutorials are great, but please could you review how often you say "go ahead" in your voiceover. This applies to all the vids, but as an example, it's said 82 times in this video (I counted), and a lowlight is at 8:46, where "we're gonna go ahead and say go ahead..." 😬
@iOSAcademy
@iOSAcademy 2 роки тому
Lol I GIVE YOU PROPS FOR COUNTING. Good feedback for me though
@nigelpstreet
@nigelpstreet 2 роки тому
@@iOSAcademy Well meant feedback, thanks for taking it as intended 😊
@nion159
@nion159 2 роки тому
once you notice it , you cannot unnotice it 😂
@user-yd9xy3rb4x
@user-yd9xy3rb4x 2 роки тому
I needed it.
@iOSAcademy
@iOSAcademy 2 роки тому
glad it helped!
@setilanaji8442
@setilanaji8442 2 роки тому
Yeaahhh finally thanks
@setilanaji8442
@setilanaji8442 2 роки тому
Do cleanse also please 🙏
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks
@TheAhmadReza
@TheAhmadReza 2 роки тому
Thank you, but I didn't understand the purpose of using it... it seems overengineering to me 🤔
@borkophil6948
@borkophil6948 2 роки тому
Agreed. I just dont see what problem this would solve. You still create SecondVC directly, its just moved in the other method.
@aird1893
@aird1893 2 роки тому
Hi - Can you do a video on DIP framework too?
@iOSAcademy
@iOSAcademy 2 роки тому
Sure
@RyokNaruto
@RyokNaruto 2 роки тому
Swift always give dependency injection free like Kotlin/Java Dagger?
@iOSAcademy
@iOSAcademy 2 роки тому
Not exactly. You need a framework or to do it manually with interfaces
@EdRawrDev5813
@EdRawrDev5813 Рік тому
I think this is great but maybe isolating files would be great too , ty
@iOSAcademy
@iOSAcademy Рік тому
Thanks for the feedback
@baonguyenngoc8783
@baonguyenngoc8783 2 роки тому
Cool!
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks
@eldarkk4566
@eldarkk4566 2 роки тому
YEAH BOY ~~~~~
@iOSAcademy
@iOSAcademy 2 роки тому
haha thanks!
@FernandoGeraldoMantoan
@FernandoGeraldoMantoan 2 роки тому
Are there any alternatives to Swinject?
@iOSAcademy
@iOSAcademy 2 роки тому
Several
@FernandoGeraldoMantoan
@FernandoGeraldoMantoan 2 роки тому
​@@iOSAcademy I saw Resolver the other day, it looks like a great library, would you suggest any others? I need to present some alternatives to other devs so we can pick one to use on a new app.
@al_mustapha
@al_mustapha 2 роки тому
Hello Afraz, I am checking in with you again about the view model problem I have been having for months from one of your lessons. You asked me to reach out to you. I have continued leaving comments on your videos so I can get a meeting with you. Please please please, can you reach out.
@oleg991
@oleg991 2 роки тому
Nice video for beginners. One question tho: why don’t you use swift package manager in such videos? New iOS developers will likely not use cocoapods on real projects.
@iOSAcademy
@iOSAcademy 2 роки тому
Cocoapods are by far way more popular in large professional projects. Like fb, google, etc.
@oleg991
@oleg991 2 роки тому
@@iOSAcademy those projects might also use objective-c in many places but it does not mean other projects also need to use old technologies like objective-c or cocoapods
@divyarai9843
@divyarai9843 Рік тому
can't access source code after paid subscription
@iOSAcademy
@iOSAcademy Рік тому
Whats your github
@Jannemandevries
@Jannemandevries 2 роки тому
Sorry but all I'm seeing is tons of complexity just to show a VC with a random color. I'd rather see videos that convince me on why I would even bother with this, so more of an actual real life example.
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks for the feedback. This video is a primer with an expectation the viewer is familiar with DI. Ill be doing more videos with more complex real world examples. Stay tuned
@RostyslavKobizsky
@RostyslavKobizsky 2 роки тому
2022 cocapods - not worth to watch to the end
@RostyslavKobizsky
@RostyslavKobizsky 2 роки тому
And UIKit omg dinosaur please stop!
@iOSAcademy
@iOSAcademy 2 роки тому
Thanks for the feedback. Just so you are aware Google, Facebook, Netfli, Microsoft, all the other big companies are still using uikit and cocoapods
@RostyslavKobizsky
@RostyslavKobizsky 2 роки тому
@@iOSAcademy I know, and you know why - Legacy! But you have no legacy, you’re free, and you can help this legacy to be gone :) and thanks for your response!
@robertj1679
@robertj1679 2 роки тому
@@RostyslavKobizsky Just because it’s legacy doesn’t mean it should be avoided. If you’re going for an iOS job most of them today are looking for UIKit with maybe some SwiftUI. So it doesn’t hurt to work with it or at least see it.
Swift: Dependency Injection Tutorial (2022) - iOS
24:31
iOS Academy
Переглядів 34 тис.
Dependency Injection
15:31
SwiftBook
Переглядів 14 тис.
How did CatNap end up in Luca cartoon?🙀
00:16
LOL
Переглядів 5 млн
Protect The Yacht, Keep It!
15:08
MrBeast
Переглядів 54 млн
Intro to Sendable in Swift 5 (2022)
11:23
iOS Academy
Переглядів 12 тис.
NSCache: Performant iOS App Basics (2022)
14:11
iOS Academy
Переглядів 13 тис.
Memory Leaks in iOS: Find, Diagnose, & Fix (2022)
12:44
iOS Academy
Переглядів 44 тис.
Swift: Actors Introduction (2023, Xcode 13) - iOS
8:54
iOS Academy
Переглядів 38 тис.
Dependency injection в Swift.
25:35
iOS Skills
Переглядів 1,5 тис.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Переглядів 713 тис.
Object IDs: Dependency Injection Fundamentals (iOS) - 2022
15:30
iOS Academy
Переглядів 4,8 тис.
How did CatNap end up in Luca cartoon?🙀
00:16
LOL
Переглядів 5 млн