How to use @EnvironmentObject in SwiftUI | Bootcamp #51

  Рет қаралды 37,457

Swiftful Thinking

Swiftful Thinking

3 жыл бұрын

The @EnvironmentObject is a super-useful property wrapper that lets us put an observable object into the environment of our SwiftUI application. By doing this, all of these views in the view hierarchy will then have access to this object. This is the perfect solution for any case where you have a class that needs to be accessed by multiple views, but you don't want to manually bind it from view to view to view with initializers.
Next video: • How to use @AppStorage...
Last video: • How to use @Observable...
🤙 WELCOME BACK 🤙
WEBSITE: www.swiftful-thinking.com
DISCORD: / discord
GITHUB: github.com/SwiftfulThinking/
SAY THANKS: www.buymeacoffee.com/nicksarno

Пікірлер: 122
@karaokedeconstruction
@karaokedeconstruction 3 жыл бұрын
Well done. I've been watching KZfaq videos all day to figure out how @EnvironmentObjects work; only yours made sense and everything clicked together. Thank you, and please continue making great videos!
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Comments like this make my day! Thanks for watching :)
@5thgenapps608
@5thgenapps608 3 жыл бұрын
Everything explained so clearly without useless , confusing refrences to other languages or how it "used to be done." I watch a lot of ios courses on udemy etc. Nobody explains anything as clearly as you. Thank you man. If you are ever doing a course on building an ecommerce application with variants like small, medium, etc(not just a coffee app) . I would join for sure and definitely would pay.
@elemento_arts
@elemento_arts 3 ай бұрын
Very well explained!. Probably naming the ViewModel differently would have made it a bit less confusing but still, very clear and concise. 👌
@hashemakkad6439
@hashemakkad6439 8 ай бұрын
This is an amazing part, it was the best part so far. Thank you Nick.
@pitmanra
@pitmanra 3 жыл бұрын
Very clear explanation. Thanks for mapping the flow of these objects through the program. That helps a lot.
@benceylan273
@benceylan273 11 ай бұрын
Yet another awesome video. Easy to understand thanks to your teaching method. Thanks again Nick.
@sols.2525
@sols.2525 3 жыл бұрын
This is my second time through your videos. The first time I wasn't really able to code anything because I had some major surgeries and wasn't able to type yet. Anyhow, this video not only helped me grasp @EnvironmentObject but also made it even more clear to me Observable and State Objects. Thanks Nick! You are definitely my favorite SwiftUI teacher out there!
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Haha thanks again Scott. I hope your recovery goes well. Glad to hear you're typing again 🥳
@antonywmiller
@antonywmiller 3 жыл бұрын
Thanks for this one! Seriously... you have a gift for this kind of instruction. Hope you keep it up.
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Thanks for the comment Antony! Will do :)
@Shahad-tj8fp
@Shahad-tj8fp 2 жыл бұрын
thank you so much ive been struggling to pass data in swiftui this is soo helpful!! you're awesome
@aqilrashid6422
@aqilrashid6422 3 жыл бұрын
Thanks for the great job you done, please continue.
@DaveJacobseniOS
@DaveJacobseniOS 3 жыл бұрын
Just found your channel and have been through a bunch of the vids cuz I’m learning SwiftUI. Thanks for all the great stuff! Really good quality👌
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Thanks for watching Dave!
@JB92212
@JB92212 2 жыл бұрын
Thanks for making it clear. I actually understand the Environment and the differences with the other property wrappers now. Your channel is making this stuff all click for me now, so grateful. Keep going!
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
:) Thanks for watching JB
@lautarob
@lautarob 7 ай бұрын
Among the swift youTube videos, these are probably the best!. Thank you so much!. The "beginner" series in swiftUI has way more useful information than many courses I've seen on Udemy, LinkedIN Learning and the like. Good Job!
@frederickjavalera558
@frederickjavalera558 3 жыл бұрын
Bravo!!! Super complex topic to explain, explained as efficiently (yet thorough) and clearly as I've been able to find anywhere.
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Thanks Frederick!!!
@TruongNguyen-mc2yt
@TruongNguyen-mc2yt Жыл бұрын
I LOVE YOUR CHANNEL! Please keep on making these awesome videos!
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
Thanks!!
@nazrulhassan6310
@nazrulhassan6310 3 жыл бұрын
fabulous video along with @environmentobject it helped me other things like scrollview , color literal sink in. thanks
@egorsev
@egorsev 3 жыл бұрын
Thank you for your lessons. You’re the best!
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Welcome to the channel and thanks for watching @egorsev!
@ricardokuster
@ricardokuster 3 жыл бұрын
Really clear and precise explanation ! Thank you !
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Thanks Ricardo! Glad you enjoyed it :)
@beemercycle
@beemercycle 2 жыл бұрын
Great Video - Very Helpful - I'm storing a link to it for future reference!
@Nunny25
@Nunny25 3 жыл бұрын
Thank You, very clear for understanding..
@indomitabletr1834
@indomitabletr1834 2 жыл бұрын
So good tutorial, Thanks dude. You made clear my mind.
@isharameegahawala9325
@isharameegahawala9325 2 жыл бұрын
Nicely explained. Thank you.
@dugrut1325
@dugrut1325 Жыл бұрын
Okay I'm back. Now I finally got it. For some reason it seems so simple, can't understand why it didn't click a year ago. Thanks for the 100th time.
@prasadnagarale2366
@prasadnagarale2366 2 жыл бұрын
You deserve more subscribers mate! amazing explaination!! gonna recommend to 4-5 of my peers.
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thank you Prasad!! 🙃 Say hi to your friends for me.:)
@priyankab6556
@priyankab6556 3 ай бұрын
i wish Nick to be in Apple WWDC team ....you are a great teacher
@ShowMeLova
@ShowMeLova 5 ай бұрын
Thanks a lot! You're the best!
@MarcSOUBEYRAND
@MarcSOUBEYRAND 2 жыл бұрын
You’re a genius of teaching !! Trank you
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thanks for watching Marc 😃
@ostanjaffcompany173
@ostanjaffcompany173 3 жыл бұрын
Man, you are a genius! Thanks a lot.
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Haha thanks for watching!!
@bbulliard
@bbulliard 2 жыл бұрын
I really love this video. Thank you. Dropping off money for coffee!
@thebrainliteracyproject1050
@thebrainliteracyproject1050 Жыл бұрын
Nice teaching style!
@MrSkyydude
@MrSkyydude Жыл бұрын
My question for you, Nick, is how did you obtain this level of understanding of SwiftUI and Swift in general?
@randlyce
@randlyce 2 жыл бұрын
wow this one is really a step up from the others
@knowledgeispower4953
@knowledgeispower4953 6 ай бұрын
Good job .. bro..
@lotpb
@lotpb 3 жыл бұрын
you are a blessing
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Happy to help Peter :)
@dimflash1987
@dimflash1987 2 жыл бұрын
it's brilliant!
@user-ev3dl9zt8s
@user-ev3dl9zt8s 2 жыл бұрын
Thank you! Thank you!! Thank you!!!
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
🤗
@eddiecarbon146
@eddiecarbon146 8 ай бұрын
Super!!
@sadeqnoori8707
@sadeqnoori8707 3 жыл бұрын
thank you man
@sathamhussain9299
@sathamhussain9299 3 жыл бұрын
That's awesome.
@sattorjabborov2728
@sattorjabborov2728 Жыл бұрын
TOP 💯🔥
@dadrewco100
@dadrewco100 3 жыл бұрын
Nice one!
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Thanks for watching Andrew!
@bbulliard
@bbulliard 2 жыл бұрын
I will now only use environment objects. You are such a gifted teacher! Thank you
@picabloo
@picabloo 2 жыл бұрын
Only when needed!
@chezchezchezchez
@chezchezchezchez Жыл бұрын
@@picabloowhy?
@picabloo
@picabloo Жыл бұрын
@@chezchezchezchez they are meant for that specific use case. They take up more memory, and it’s not good coding practice to have things exposed to everywhere. Even when coding on your own, try to have a sense of organization and expose variables/objects only when needed.
@chezchezchezchez
@chezchezchezchez Жыл бұрын
@@picabloo do these teachers on KZfaq that explain how it works talk about the dangers of it using up more memory? Also, don’t we have a ton of memory in our powerful phones nowadays what’s a little memory when you have the convenience of an “everywhere” variable?
@picabloo
@picabloo Жыл бұрын
@@chezchezchezchez There are pros and cons. I am simply giving standard coding advice. In a coding interview, you will lose points for declaring everything as environment, but that’s your choice.
@d42
@d42 Жыл бұрын
the color literal doesn't seem to exist anymore. could you make a video on that?
@samisanoof
@samisanoof 2 жыл бұрын
Thank you for making the developers' life easier. I have one query, is it possible to use an environment object in any ModelView ?
@guzmangonzalezrodrigo6268
@guzmangonzalezrodrigo6268 3 жыл бұрын
awesome!! (:
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Thanks Rodrigo!
@Toxls12980
@Toxls12980 Жыл бұрын
ty
@kal5211
@kal5211 Жыл бұрын
My man.
@nickfloussov185
@nickfloussov185 9 ай бұрын
Video is good as usual, and IMHO it would be even better if Nick mentioned word GLOBAL as concept behind 'EnvironmentObject' P.S. and also not clear why Nick used wrappers like @StateObject and @ObservedObject.
@comexbackkid
@comexbackkid 2 жыл бұрын
Ok, damn. I've been having trouble understanding property wrappers but the way you've explained them & outlined this bootcamp series is insanely easy to understand. Thank you thank you thank you. Quick question, it seems that with all the SwiftUI tutorials I've been watching online, people start to code all in one file and then slowly build out the MVVM architecture as they go. Is that for simplicity's sake with regards to the instruction, or is that common practice in the field?
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Great question! I think this is probably for simplicity sake in tutorials. In most apps, you'll want to separate your Views and ViewModels in individual files from the start!
@mahtabhasan8513
@mahtabhasan8513 2 жыл бұрын
Both video about StateObect, ObeservedObject and EnvironmentObject really explained very nicely. I have washed your many videos about SwiftUI, found all helpful in my App development. Thanks Nick.
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thanks for watching Mahtab!
@filipp7236
@filipp7236 4 ай бұрын
Hi! Great video and a simple explanation. What if, instead od Views - I have few View models and one use other logics. Do I have to pass that ViewModels there same like in Views?
@sriratmurti1345
@sriratmurti1345 2 жыл бұрын
Very clear! Thank You👍
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
Thanks Sri!
@dugrut1325
@dugrut1325 2 жыл бұрын
I want to understand the... "So Cool!" After my third watch of this concept, I think I have it, but I know I'll lose it as soon as I start coding, hahaha. To think I use to be a VBA coder 15 years ago.
@brucknerdevilliers
@brucknerdevilliers 2 жыл бұрын
Fantastic video - it resolved a lot of confusion. However, I tried adding a .navigationTitle to the Second Screen(before the NavigationLink closure), which appeared on the Back button of the 3rd Screen, but 2nd screens back button reverted to 'Back' instead of the title of the first screen. Any thoughts?
@aminechatate8487
@aminechatate8487 3 ай бұрын
Hi from morocco, thank you for your tutorials, I have one problem : In the finalView() I want to change a property (name for exp) of the object selected, when I return Back to DetailVew() and EnvironementObjectBootcamp() I need to see that change.
@BradCaldwellAuburn
@BradCaldwellAuburn 8 ай бұрын
Nice, I was hopeful, But alas it still says 'cannot find viewModel in scope.'
@kdtechniquesofficial6153
@kdtechniquesofficial6153 2 жыл бұрын
Swift UI Angel 😘🥳🥳🥳🥳
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
KD 🥰
@netfreak9
@netfreak9 3 жыл бұрын
Explained really well, The object we need can be injected into the environment from a specific place in the view(like you did after the NavigationView closure) or it can be injected from anywhere? Any reason why after the NavigationView?
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Hi Netfreak9! The object can be injected from anywhere, but it needs to be injected into the hierarchy BEFORE you want to use it. So it needs to be added a parent object. I put on the NavigationView so that all of the children within the NavigationView will have access to it. I find that most of the time I use this as the beginning of my app's view hierarchy.
@frankopokukoduah194
@frankopokukoduah194 5 ай бұрын
How does it work when the second screen and third screen are on another swiftui sheet, do we have to bind the variables?
@faikcoskuncakr7418
@faikcoskuncakr7418 2 жыл бұрын
Hi Nick, How can I access an environment object from within a function. Because I can't get instance from environment class as in structs there.
@ChasedByPenguins
@ChasedByPenguins 2 жыл бұрын
So the @EnvironmentObject is an awesome way to provide your data through the Views, now my question is: How do I persist the data into some kind of a data store like Core Data or then intp iCloud so that it syncs across my devices?
@Hamly88
@Hamly88 Жыл бұрын
I almost gave up on swift because of @environment, thank you so much, only one point if you can explain if I have: 1. Model: Data is here! 2. ViewModel: connection between Model & View 3. View: UI so I will add "observableobject" in the view models only in the case of your example, and other views will use this as an environmental object when needed?
@maguirer
@maguirer 3 жыл бұрын
This is a great follow-up video from the last one, but I have a question. Now that FinalView has a property wrapped by @EnvironmentObject, how would you preview it?
@SwiftfulThinking
@SwiftfulThinking 3 жыл бұрын
Hi Bob, you need to also add the .environmentObject to the preview as well. I actually do this in the next series when we build a Todo list (kzfaq.info/get/bejne/rauWZLyEmdzdh2w.html)... I think you might have already started it haha
@maguirer
@maguirer 3 жыл бұрын
@@SwiftfulThinking I figured it was probably going to be something like that. You're right, I have. Thanks. I'll keep an eye out for it.
@federicoramos
@federicoramos 2 жыл бұрын
Is it good practice to have only one VM per App?
@rajdhakate617
@rajdhakate617 Жыл бұрын
Just for some bizarre reason- if we need to pass two objects of same type, like two EnvironmentViewModels, down into the view hierarchy. Then how will we fetch the individual objects from environment? Thanks for the very informative video!
@chezchezchezchez
@chezchezchezchez Жыл бұрын
Hello! Did you happen to find out? I’d like to know also, thank you!
@EnglishTurkishExpert
@EnglishTurkishExpert Жыл бұрын
Hi there! At 24:28 you use Text, but I want to use TextField and Toggle (values of which is being fetched from a server) Since e.g. Toggle requires Binding but list of data (coming from an environment variable) supplies Bool value, I cannot set initial value of the Toggles and TextFields of the dynamically created list. Do you have any suggestion as to how to set the initial values? TIA
@chezchezchezchez
@chezchezchezchez Жыл бұрын
Hello! Did you happen to find out? I’d like to know also, thank you!
@EnglishTurkishExpert
@EnglishTurkishExpert Жыл бұрын
@@chezchezchezchez I had to learn swiftui from the scratch. You need to create subcomponents (i.e. views) to use state variables. However it is very difficult for me to be honest.
@surfdado
@surfdado 2 жыл бұрын
Great video, thank you. Any idea how I can access environment objects from other independent classes? I have a corebluetooth class that is way too large, but when I split out the data processing into a separate class I can’t figure out how to pass environment objects to that class
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
EnvironmentObjects should be accessed from the Views. If you have other classes that need to access this object, maybe it shouldn't be an EnvironmentObject, but rather just another class. You can make it a Singleton or use DependencyInjection to pass it around your app.
@surfdado
@surfdado 2 жыл бұрын
@@SwiftfulThinking thank you so much for the reply. I will definitely look into Dependency Injection, which sounds like it is exactly what I was looking for, thanks!!
@surfdado
@surfdado 2 жыл бұрын
@@SwiftfulThinking Haha so I just realize DependencyInjection is what I had been doing all along... My problem is that I need EnvironmentObjects for the different views to access various models, and then I have CoreBluetooth and my ProtocolHandler class. The ProtoHandler gets raw data from BLE and needs to feed curated data into the Models... Maybe I do need to explore the Singleton route after all...
@surfdado
@surfdado 2 жыл бұрын
but how (and also when) does my Singleton get references to the models so it can update the data? That's the part I can't figure out
@SwiftfulThinking
@SwiftfulThinking 2 жыл бұрын
@@surfdado Dependency Injection is more dynamic than a Singleton, so that shouldn’t be the solution… unless your DI isn’t set up correctly. Setting up a good architecture is one of the hardest topics in app development 🥺
@kenturnbull9679
@kenturnbull9679 Жыл бұрын
IF an add screen was part of the view hierarchy, does it also have access to the view model... or is that illustrated in the ToDo List???
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
It’s the developers choice whether you want to share your ViewModel across multiple views. In the ToDo list, that’s exactly what we will do. However in larger production apps, I usually want to keep the views independent of each other.
@gogog119
@gogog119 Жыл бұрын
why in final view it have a error "Struct 'EnvironmentObject' cannot be used as an attribute"???
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
EnvironmentObjects must conform to ObservableObject which requires the object to be a ‘class’ type (not struct)
@gogog119
@gogog119 Жыл бұрын
@@SwiftfulThinking My fault . I named the object "EnvironmentObject", so it caused this error.
@mahtabhasan8513
@mahtabhasan8513 2 жыл бұрын
Watched not washed - correction
@Away88123
@Away88123 10 ай бұрын
📚💬
@AntiSmithhh
@AntiSmithhh Жыл бұрын
there is no safe way to EnvironmentObject. Actually it's buggy and redundant wrapper. it's more simple and safe to use ordinary Singleton pattern where we are able to free use protocols and polymorphism.
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
This is not true
@AntiSmithhh
@AntiSmithhh Жыл бұрын
@@SwiftfulThinking OK, as you wish.
@AntiSmithhh
@AntiSmithhh Жыл бұрын
​@@SwiftfulThinking I've renewed tests. Polymorphism is working correctly now. In that part I was not right now. But main problem is still here.
@SwiftUA
@SwiftUA 2 жыл бұрын
Hard to understand
@user-qi3rb3zc7e
@user-qi3rb3zc7e Ай бұрын
I found in the DetailView and the FinalVIew ,delete the "@ObservedObject",there's nothing different for the result! So, I'm confused, whether must add "@ObservedObject"?。Forgive my English is not good, hahaha
@preuton
@preuton Жыл бұрын
When i remove @Published, @StateObject (first view) and @StateObject in DetailView struct the app keeps working as expected. 🙄
@SwiftfulThinking
@SwiftfulThinking Жыл бұрын
You should only have 1 @StateObject for each ObservableObject. @StateObject is what decides which View should manage the state of the object as it’s passed around your app.
How to use @AppStorage in SwiftUI | Bootcamp #52
17:08
Swiftful Thinking
Рет қаралды 28 М.
How to use @ObservableObject and @StateObject in SwiftUI | Bootcamp #50
35:12
Sigma girl and soap bubbles by Secret Vlog
00:37
Secret Vlog
Рет қаралды 15 МЛН
Alex hid in the closet #shorts
00:14
Mihdens
Рет қаралды 18 МЛН
Ouch.. 🤕
00:30
Celine & Michiel
Рет қаралды 26 МЛН
Как бесплатно замутить iphone 15 pro max
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 8 МЛН
How to use Do, Try, Catch, and Throws in Swift | Swift Concurrency #1
26:34
How to use @ViewBuilder in SwiftUI | Advanced Learning #9
24:01
Swiftful Thinking
Рет қаралды 25 М.
JPEG is Dying - And that's a bad thing
8:09
2kliksphilip
Рет қаралды 60 М.
@EnvironmentObject Explained in SwiftUI | iOS 17
21:16
AppStuff
Рет қаралды 5 М.
What's going on with Windows Laptops?
10:30
Marques Brownlee
Рет қаралды 2,7 МЛН
@EnvironmentObject vs @Environment(_:): how do you choose? 🤔
11:29
Vincent Pradeilles
Рет қаралды 1,7 М.
Sigma girl and soap bubbles by Secret Vlog
00:37
Secret Vlog
Рет қаралды 15 МЛН