Swift Dependency Injection - What is it? What are the benefits?

  Переглядів 19,659

Sean Allen

Sean Allen

День тому

Go to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
Dependency Injection is when you give an object what it needs, rather than it creating those objects itself. In this video I show you a basic example using the Chipotle App as an example. And then I help you answer the common iOS Dev interview questions asking why would you use dependency injection in Swift and what are the benefits.
This tutorial was created using Xcode 14 and iOS 16.
My iOS Dev Courses:
seanallen.teachable.com/
iOS Developer Interview Questions Playlist:
• Swift Interview Tips
Twitter:
Sean Allen - / seanallen_dev
Hired.com:
hired.com/x/1n01g
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Paul Hudson's Hacking With Swift:
gumroad.com/a/762098803
Donny Wals - Combine:
gumroad.com/a/909014131
Mark Moeyken’s SwiftUI Books:
www.bigmountainstudio.com/swiftui-views-book/fzc51
Objc.io Books (Thinking in SwiftUI & Advanced Swift):
gumroad.com/a/656585843
#swift #softwaredeveloper #iosdeveloper
Timestamps:
0:00 - What is Dependency Injection
0:26 - Initializer Injection Code Example
4:19 - Benefits of Dependency Injection

КОМЕНТАРІ: 33
@seanallen
@seanallen Рік тому
If you like my teaching style check out my iOS developer courses at seanallen.teachable.com
@merouanebboy
@merouanebboy Рік тому
Somehow I ended up being the first view of this video. I started as an iOS developer following Sean's interview videos 5 years ago, I ended up being a Backend/Cloud developer now. by viewing this video I realize how much I changed and how much Sean's video got better over the time. Keep it up Sean, looking at these swift videos make me want to switch back somehow to iOS developement.
@seanallen
@seanallen Рік тому
Blast from the past! I cringe a little bit looking back at those videos. Come on back to iOS Dev - it's fun 😀
@teaberry73
@teaberry73 Рік тому
you know what, Sean you are awesome. I was confused about what dependency injection was until the first 20 seconds of the video. Can't thank you enough!!
@seanallen
@seanallen Рік тому
Happy to help 😀
@tchendoh
@tchendoh Рік тому
Great vid! Nice to confirm what I thought I understood with better words. BTW, I like the very short simple code technique so we can focus on the theory.
@seanallen
@seanallen Рік тому
Happy to hear you liked it. I try to remove as much non-topic stuff from my videos to keep them focused. When I was learning I found this to be clearer for me as I wouldn't get confused by all the other things going on in the code. Glad you like that style too 😀
@tylerwatt5651
@tylerwatt5651 11 місяців тому
THANK YOU SO MUCH!!!
@JasonMitchellAZ
@JasonMitchellAZ Рік тому
Perfect timing for this example. I just had Chipotle today. That double meat chicken al pastor was good. 😋
@seanallen
@seanallen Рік тому
I'm a double steak guy, myself.
@captainmichaelj2321
@captainmichaelj2321 Рік тому
Absolutely fantastic video as usual, would have been even more valuable for me if you had more content on the "easier testing" I use the dependency injection to pass my view models across different views, but never quite had the practice to use it for "easier tests", as I always just use the simulator build, not llb debugging, but would have ben great to learn more about that. Thanks for the great content as usual!
@cristopherescorcia3276
@cristopherescorcia3276 Рік тому
When running unit test you don’t want to use your real viewmodel to network calls and logic, instead you create a mock, stub, whatever you need, and then in the unit test you pass that mock or stub, and this wouldn’t be possible by letting your views create their own dependencies.
@seanallen
@seanallen Рік тому
Glad you enjoyed the video! See Christopher's answer in this thread for your test question.
@captainmichaelj2321
@captainmichaelj2321 Рік тому
@@cristopherescorcia3276 thanks so much for the detailed response! appreciate it.
@cristopherescorcia3276
@cristopherescorcia3276 Рік тому
Great video as always Sean, just one thing in the company I work we prefer to use the init injection instead of the property wrapper that Resolver provides, don’t get me wrong the property wrapper work fine and it’s probably easier, but we prefer the init to avoid couple our app to that library in case anything went wrong and the library stops working, in the case that happen we just can remove it and continue working with initializers.
@seanallen
@seanallen Рік тому
I've never used Resolver so I can't comment on that. When I use dependency injection, 90% of the time it's initializer injection.
@vindajojo
@vindajojo 4 місяці тому
big thanks!
@seanallen
@seanallen 4 місяці тому
Happy to help!
@minimalSwift
@minimalSwift 9 місяців тому
awesome!!! :)
@alenayoutube1574
@alenayoutube1574 Рік тому
Good video
@seanallen
@seanallen Рік тому
Glad you liked it!
@vittyfiorentini238
@vittyfiorentini238 Рік тому
Hi, updating xcode to 14.3 i receive the log "[PipelineLibrary] Mapping the pipeline data cache failed, errno22" when adding the MapView in the viewcontroller. I Receive this error only when i test on a real device not in the simulator. Can i fix it ? Is a common problem? Thanks for the time I appreciate so much.
@fsdolphin
@fsdolphin Рік тому
Is there any downside by creating the new object directly when passing it to the child class? like this... `let viewModel = BurritoIngredientsViewModel(networkManager: NetWorkManager(), bag: Bag())` Less code. Thanks a lot for all of your videos.
@ahmetbugraozcan6742
@ahmetbugraozcan6742 Рік тому
What is the difference between this and Combine's @EnvironmentObject? Isnt it good to use that wrapper instead of using parameters? I am confused sometimes about that.
@seanallen
@seanallen Рік тому
@EnvironmentObject is specific to SwiftUI. If you're using SwiftUI @EnvironmentObject can be a good solution when sharing data between views (again, it's situational). I kept my code in the example abstract and vague because there are so many different ways to pass data depending on the situation (UIKit vs. SwiftUI, the types of objects, etc...)
@tbg6nb
@tbg6nb Рік тому
If you put something in the environment using the @EnvironmentObject, it will be available to each child view after you pass it to the top-level view
@bonifacy9511
@bonifacy9511 Рік тому
@Sean Allen, why not to pass VM's via .environmentObject?
@seanallen
@seanallen Рік тому
Yes, in SwiftUI passing data via an EnvironmentObject is the likely solution. I mention in the video that my code was "pseudocode" because how you pass the data depends on the situation (UIKit vs. SwiftUI, or the type of objects, etc...), so I kept it abstract to keep that part of the video focused on the Initializer Injection. I didn't want to get bogged down explaining all the various ways to pass the data in various situations.
@sumitrathore8446
@sumitrathore8446 Рік тому
How auto complete for class init?
@seanallen
@seanallen Рік тому
I believe thats a new feature in Xcode 14.
@brandtdaniels
@brandtdaniels Рік тому
Let’s not normalize ‘ER’ names like Manager. It represents a lack of understanding of the actual domain. Also, what Apple is calling Protocol Oriented Programming, is actually just Object Oriented Programming as Protocols (Interfaces in Java) represent Roles in OOP. An object should have a dependency on some sort of Role that it defines, which will also help with naming!
@smarter2610
@smarter2610 Рік тому
5 years ago I started learning SWIFT and it was the worst decision in my life, this market is so small for juniors that it was such a big waste of my time ..... today I am devops with python/bash etc and I am very happy with that . I remember like I was learning from you videos , liked it but the market overall is shitty in my honest opinion, especially like technology like REACT evolved and would not recommend to anybody go to the mobile sector. It was very good in the begining, today its time of cloud/security/devops/ai not mobile apps.
@Hgvpuncher
@Hgvpuncher 2 місяці тому
depends in what country you live in... For example iOS juniors in UK are required everywhere at the base salary starts from £55k-70k
Swift Basics: Array vs. Set
9:32
Sean Allen
Переглядів 18 тис.
Swift Closures Explained
14:23
Sean Allen
Переглядів 56 тис.
GADGETS VS HACKS || Random Useful Tools For your child #hacks #gadgets
00:35
Эффект Карбонаро и устройство для распаковки
01:00
История одного вокалиста
Переглядів 2,4 млн
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Переглядів 710 тис.
Getting started with iOS modularization by Luka Terzić
23:48
Infinum
Переглядів 3,3 тис.
Dependency Injection
15:31
SwiftBook
Переглядів 14 тис.
Dependency Injection Explained in 7 Minutes
7:11
ArjanCodes
Переглядів 44 тис.
Swift: Dependency Injection Tutorial (2022) - iOS
24:31
iOS Academy
Переглядів 34 тис.
Swinject: Effortless Dependency Injection in iOS (Basics)
12:45
iOS Academy
Переглядів 27 тис.
🤏 САМЫЙ ТОНКИЙ гаджет #Apple! 🍏
0:29
Яблочный Маньяк
Переглядів 513 тис.
Creepy Samsung Alarm cannot be turned off 😱🤣 #shorts
0:14
Adani Family
Переглядів 706 тис.
Какой телефон лучше всего снимает? 🤯
0:42