How to use @ObservableObject and @StateObject in SwiftUI | Bootcamp #50

  Переглядів 54,954

Swiftful Thinking

Swiftful Thinking

3 роки тому

As a SwiftUI developer, you are probably well aware of the @State property wrapper. We use this to tell the View that it will need to update if a @State variable changes. This is create for basic data types, but unfortunately, does not work for other classes in our application. Thankfully, the SwiftUI framework includes the @ObservableObject and @StateObject property wrappers for this purpose. In this video we will create "view model" which is a custom class that will manage the data for our View and then implement both of these new property wrappers into our iOS application.
Next video: • How to use @Environmen...
Last video: • How to create custom m...

КОМЕНТАРІ: 195
@daraghmacgabhann1005
@daraghmacgabhann1005 3 роки тому
Your course is excellent, beats all others I've seen hands down! Your course/videos clearly show how to use the different items with thoughtful examples and explanations. This video explains what I found to be the most confusing, when to use @StateObject and @ObservableBbject. Looking forward to the Environment video. Thank you for taking the time and effort to create this course, it sets the gold standard!
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Thanks Daragh! I really appreciate that. Yes, the @EnvironmentObject is next... if you understood this video, the next one should be a breeze. Thanks for watching :)
@picabloo
@picabloo 2 роки тому
I am SO happy I found your channel. Amazing quality. Great clear communication, sympathy for newbies, explaining everything, quality mic, everything. Thanks for all your hard work!
@vazzhh
@vazzhh 3 місяці тому
So do I
@Wiintb
@Wiintb Рік тому
Nick. I am a CTO who was trying to learn Swift for fun. I had visited a lot of material and I find your approach to be the best. Beats even the paid materials by a long distance. My best wishes and do let me know how we can help contribute to you besides referring. You should be hitting at least 100K viewers.
@muhammadusman-vc5re
@muhammadusman-vc5re Рік тому
Not only describe the concept in a straightforward way but also tells the use cases of real projects. GOD bless on you.
@solomonrajkumar5537
@solomonrajkumar5537 Рік тому
your boot camp is incredible awesome!!!
@stefanlindholm3284
@stefanlindholm3284 2 роки тому
This was very well explained! Thank you Nick!
@jameshawley2745
@jameshawley2745 Рік тому
Dude these tutorials are amazing! Keep up the good work!
@bbulliard
@bbulliard 2 роки тому
I love the way you explained the diff between @state and observable object. So abundantly clear
@ehsanbaghaki
@ehsanbaghaki Рік тому
I have watched over 10 different tutorials on youtube around this concept, you have explained this concept much much much better and cleaner and underestandable than any other tutorial. Thanks a lot!
@sols.2525
@sols.2525 3 роки тому
I have been watching your videos for a few days now. I also watch some others content creators as well. Out of all the ones I watch I find yours to be excellent and unique. You are the first person to actually make videos explaining each thing in great detail. All the others just build apps that get followed with maybe some explaining. Thats great too but this is helping me learn so much better.... So Thank You for making all of these and I hope you never stop!
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Wow! Thanks for the comment Scott. That's really appreciated. I'm very happy to help. Let me know if you have any questions!
@OGLerma
@OGLerma 2 роки тому
Amazing explanation! Love all your videos. I was a little confused before and this kinda cleared everything up.
@stepanivanov6877
@stepanivanov6877 2 роки тому
Thank you Nick. You explained how to use @StateObject and @ObservedObject perfectly.
@jlambert12013
@jlambert12013 10 місяців тому
Best explanation of these property wrappers. I’ve spent months trying to grasp managing state and it has been so confusing. This tutorial wasn’t so overly complicated like so I actually grasped the concepts. This entire channel is GOLD!
@AllanZhang16
@AllanZhang16 5 місяців тому
What a great video. Perfect pacing, perfect explanations, and I like how you reinforce important concepts. Thanks again.
@aloksinha2688
@aloksinha2688 Рік тому
Your explanation techniques is unique and simple..I love it. 👍
@rajdhakate617
@rajdhakate617 Рік тому
Man really solved my doubt about @StateObject and @ObservedObject. Please make sure videos like these. It’s amazing
@alanmaloney2791
@alanmaloney2791 7 днів тому
Brilliant video! Wonderfully clear and perfectly explained👍👍👍
@rafaelclaycon
@rafaelclaycon 2 роки тому
Omg, you gave me the missing piece I was looking for! Thank you so much! Great content!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks!
@bbulliard
@bbulliard 2 роки тому
Wonderful for understanding state, stateobj and observable object. Thanks a bunch
@tamasterjek7520
@tamasterjek7520 2 роки тому
First video I watched from you. Great explanation, easy to understand. Now I have to watch your other videos too :)
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Welcome to the channel Tamas! I hope you like them :)
@tigran.zakaryan
@tigran.zakaryan Рік тому
Great explanation! Thank you👍👍👍👍
@zcaur7329
@zcaur7329 2 роки тому
What a well done video ! Kudos to you !
@ataberkkilinctekin188
@ataberkkilinctekin188 Рік тому
This video explains a lot about the @ObservableObject and @StateObject. I literally learned what those are and how they are used. Thanks a lot for this video. :)
@gregh528
@gregh528 6 місяців тому
Great video. Thanks Nick.
@idrissben1613
@idrissben1613 Рік тому
thank you it's by far the best explanation i found for this subject!
@filipp7236
@filipp7236 Місяць тому
You're the best, dude! I've been doing this Hacking with Swift course, but I was so confused with the fundamentals, and you are explaining it so simply! I'm very grateful. Thanks.
@aleksandertesenkov1421
@aleksandertesenkov1421 3 місяці тому
Excellent video. Wonderful explanation. I knew most of this stuff but I learned many small details.
@adamkacha3823
@adamkacha3823 2 роки тому
Great explanation. Thank you so much!
@SergeiKeyVee
@SergeiKeyVee 2 роки тому
Thank you so much! I finally got it. Amazing video)
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
You're welcome Sergei!
@nevemra5
@nevemra5 11 місяців тому
Perfect, perfect, perfect! Every day I learn from your videos. You were not the first in my path, but by far the best. I have never understood complex topics so easily, as I began to understand with you. Clear beautiful speech allows me to understand you (English is not my native language). I'm very happy that I found you. I feel that one mug of coffee will not be enough)) The material costs a whole mountain of mugs of coffee
@olepetersen4460
@olepetersen4460 2 роки тому
Brilliant - you just cleared up for me, why my app reloaded data at times, when I did not want it. And what to do about it. Many thanks.
@kauekaj
@kauekaj 8 місяців тому
Awesome! 👏
@user-bw8um3ex7t
@user-bw8um3ex7t 2 місяці тому
Thanks from the bottom of my Heart Mate ❤💚
@morreke
@morreke 2 роки тому
Very well explained !!
@sundayreading
@sundayreading Рік тому
Proud coffee member on a monthly basis now🥳. Please do not stop teaching!
@SwiftfulThinking
@SwiftfulThinking Рік тому
Thanks! More vids coming soon 👍
@mustafasabahy3092
@mustafasabahy3092 Рік тому
please don't stop you make life easier and more simple ♥
@douglasfers7
@douglasfers7 4 місяці тому
Dud you're amazing! Thanks a lot!
@iziman99
@iziman99 2 роки тому
Great vid as always. Keep it up! Loving this content and leaving comments to make your YT algorithm better!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thank you Tomek! That’s very much appreciated 🥳
@valerieokelly6224
@valerieokelly6224 Рік тому
Top quality course 😍
@mamazur
@mamazur 2 роки тому
All courses are amazing. You are the best.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thank you for watching :)))
@mamazur
@mamazur 2 роки тому
@@SwiftfulThinking Nick your course is super. But if you have time can you make another video about Picker. It is not working, especially label. And by the way how to make picker with optional value. I think about such pickier but I don't know how to make it In such nice and user friendly look like you do. Thanks for all. The best iOS course on internet is yours. You are legend.
@maguirer
@maguirer 3 роки тому
Another great video, Nick! This is probably the most crucial one to date in the Bootcamp series, IMHO. I have a feeling I'll be coming back to it again several times. I've watched a lot of the WWDC videos, but they still left me a bit confused on what the difference was between @StateObject and @ObservedObject and when to use one over the other. In fact, I think I had them backwards in my head, but this video has helped them click into place.
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
I'm very happy to help this. This was confusing for me as well when I was learning. Once you understand it, it's actually pretty easy to implement. It's definitely important though because we will use these for all of our ViewModels in MVVM architecture!
@itsAJ819
@itsAJ819 2 роки тому
Im in the same boat. I've been watching videos on @StateObject and @ObservedObject from other creators and they left me extremely confused. Though, after watching Nick's videos on @StateObject and @ObservedObject once, its a bit clearer. Ill still have to come back and watch it a few times. Other topics, he cleared up in 1 shot for me. Nick has my favorite creator for swiftUI. I wish I watched these videos before tackling DogGram though. But once I'm done here, ill redo it.
@burakbozdoganli
@burakbozdoganli Рік тому
Excellent lessons !
@SwiftfulThinking
@SwiftfulThinking Рік тому
Thanks! 😃
@Nunny25
@Nunny25 2 роки тому
Great explanation, thanks
@andresraigoza2082
@andresraigoza2082 2 роки тому
Wonderful explanation!!! Thank you so much for investing your time making this videos.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
🙃
@Dragonboh1
@Dragonboh1 4 місяці тому
keep doing what you doing man
@dineshdas9983
@dineshdas9983 Рік тому
Sooo good explanation thanks mate🎉👍
@hithereedhere6243
@hithereedhere6243 Місяць тому
This video saved my life. Thank you.
@heshamabdo6024
@heshamabdo6024 Рік тому
There are so many great ideas in this video thnk you
@OttoFaerber
@OttoFaerber 2 роки тому
Great, your teaching method is very understandable and helps me a lot, thanks a lot
@scottw1734
@scottw1734 2 роки тому
Really well explained :)
@ScaleCodingSkills
@ScaleCodingSkills Рік тому
I started working on SwiftUI and I got stuck at @State and @StateObject, I found your video on the @StateObject, I found this very helpful and now I am planning to learn SwiftUI from your playlist instead of other platforms. Thanks for creating excellent content.
@danielcovaci6930
@danielcovaci6930 Рік тому
great tutorial, keep up the good work
@Raahim
@Raahim 2 роки тому
very well explained!
@maria_chemerys
@maria_chemerys 3 місяці тому
Thank you! You are our lifesaver❤
@pazuzutru-truluv7094
@pazuzutru-truluv7094 2 роки тому
Once again I must thank you for this video. It explains things so well where as other videos, that got me started, excluded key points and ended up in a mess when I attempted to create the project I have been tasked with. I am just about to embark on updating my app and feel confident that this has got me over the rather confused situation I had ended up in. I almost feel guilty for getting all your tutorage for free and hope you are generating some revenue from this marvellous series. Thank you once again!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks Pazuzu!
@bbulliard
@bbulliard 2 роки тому
You do such an absolute great job explaining the code. I will now have to give you the title, SMSMI (supreme master swift mentor and instructor). Thanks for a world class video. Bob
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Haha that's a new one 😂 Glad you're enjoying the Bootcamp!
@ostanjaffcompany173
@ostanjaffcompany173 2 роки тому
Great video! Thanks.
@user-ev3dl9zt8s
@user-ev3dl9zt8s 2 роки тому
Great Video. Thank you !!!
@MGFACECOM
@MGFACECOM 2 роки тому
This is one the best and the most interesting SwiftUI course... so thanks You so much.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks Ray!
@johnr.5475
@johnr.5475 2 роки тому
Another excellent video. Thanks very much. It’s very well explained.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks for watching John!
@farescherni6946
@farescherni6946 2 роки тому
sir congratulations you have the best course covering swiftui
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Wow, haha. I don't know if it's the best, but I'm very glad you're liking it! 🙃
@zhong2341
@zhong2341 2 роки тому
Thank you so much for great explanations
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
😁😁
@juich337
@juich337 2 роки тому
The Best! Thanks!
@MohamedAli-kg5im
@MohamedAli-kg5im Рік тому
thanks man, fantastic video
@SwiftfulThinking
@SwiftfulThinking Рік тому
Thanks for watching Mohamed! 🤙
@user-kn6fg4yv2w
@user-kn6fg4yv2w 6 місяців тому
Thank U! I am new on programming
@mig12394
@mig12394 3 роки тому
Thanks a lot very clear with an exhaustif example
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Glad it was helpful!
@r_celikkaya
@r_celikkaya 2 роки тому
I discovered you a few ago . I think that your videos is useful very .Thanks.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks for watching rclk!
@muqadasali8288
@muqadasali8288 2 роки тому
Oh man you are a great teacher love from Pakistan
@MikeyGio777
@MikeyGio777 Рік тому
Very Good!!!
@sema_f
@sema_f 2 роки тому
We ❤️ to hang with u here! 🥳
@JZ-xc8hu
@JZ-xc8hu Рік тому
thank you very much
@user-dj5ki4km8c
@user-dj5ki4km8c Рік тому
Additional notes for Swift beginners: * At 14:08. If the code is too long, you can remove ': FruitViewModel' from the var declaration, because type of the var is always FruitViewModel.
@redax3866
@redax3866 7 місяців тому
Do not do this. This will worsen the readability of the code.
@NICK4LIFE11
@NICK4LIFE11 4 місяці тому
The type is inferred from the assignment. Saying it will worsen the readability is your opinion. @@redax3866
@sadeqnoori8707
@sadeqnoori8707 2 роки тому
Super! thx man
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
You're always welcome 🥳
@benceylan273
@benceylan273 2 роки тому
Hey Nick, thanks for another amazing video. I said that many times but will say here again; I watched bunch of tutorials about @ObservableObject, @ObservedObject and @StateObject but this is the first time I understood the logic. Your teaching method is just awesome.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Thanks for the comment Ben! Hearing that makes this all worth it for me. I'm glad this video helped 🥳
@manishparihar3361
@manishparihar3361 9 місяців тому
Finally i got clear concept of why, when and where we can use Identifiable, ObservaleObject, @StateObject, @ObservedObject. Thank you Nick
@osi0.111
@osi0.111 2 роки тому
you are the best
@dmitriyfedorov5728
@dmitriyfedorov5728 2 роки тому
Hey Nick, thank you for the amazing video - it is very handy. I have maybe a dumb question. If you refactor the entire List into a separate struct and then use the reference to this struct in your home view. Where should @StateObject be? In the home view, right?
@elliotknight5552
@elliotknight5552 2 роки тому
My brain is burning lmao thanks for videos bro btw
@zyl44444
@zyl44444 2 роки тому
I love it!!!!!!!!!!!!!!!
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
:))))))))))
@rmjalejandro
@rmjalejandro 7 місяців тому
Nick i've finished both swift and swiftui bootcamp, came back to this vid to grasp a little bit more on this, thank you for your good work, please let us know if you have a site to "Buy you a coffee" or somehow retribute some of the knowledge you've given to us.
@SwiftfulThinking
@SwiftfulThinking 7 місяців тому
I’m glad these videos helped! I do have a BuyMeACoffee: www.buymeacoffee.com/nicksarno Thanks 🍻
@itmeocon25
@itmeocon25 Рік тому
Thank you your video. How to use button delete instead onDelete() for List ?
@jmd62800
@jmd62800 9 місяців тому
😋👏👏👏 Very good 👌
@unknownerror999
@unknownerror999 19 днів тому
Nick, thank you so much for your lessons, do you have core data lessons? i did't find it
@SwiftfulThinking
@SwiftfulThinking 18 днів тому
Yes, they are in the Continued Learning playlist. You can also search “core” across all videos here: www.swiftful-thinking.com/search
@Raahim
@Raahim 2 роки тому
underrated
@jalapisco
@jalapisco Рік тому
Thanks again for an excellent video. You did (as always) and excellent job explaining classes. I do have a question though, in SwiftUI what is the difference between structures and classes; they look pretty similar at first glance (I don't know if classes can also be used to create views but other than that one may think they can be used interchangeably).
@SwiftfulThinking
@SwiftfulThinking Рік тому
Hey! This is not a beginner topic, but everything you need to know about structs vs classes is here: ukposts.info/have/v-deo/ZXp9maSKi4F2xHU.html
@jalapisco
@jalapisco Рік тому
@@SwiftfulThinking Thanks for your response. I had a quick glance at the video and realize that it is too advanced for me so I'll be patience and wait until I get there.
@SwiftfulThinking
@SwiftfulThinking Рік тому
@@jalapisco haha for right now, just know that in SwiftUI, all of your Views must be structs and your ViewModels (or any ObservableObject) must be a class. Your data models can be either, but usually are structs.
@eddydon1929
@eddydon1929 2 роки тому
can you make a video on firebase or Vapor
@udesai2011
@udesai2011 2 роки тому
This is about a ShoppingList app I am working on. I select certain items by tapping on a check box from MasterList and append to a viewModel class instance with the selected Items. I want to populate the list on the next View when a button on ToolBar status on the master List View is clicked. I have separate viewModels for MasterList and SelectedList. But the selected List does not show any items in the second View though items are appended. How do I initialize the selectedList ViewModel on the child view. Thanks for the bootCamp videos. For now, I am passing the selected Items array in NavigationLink and initializing the selectedListViewModel with this array in View2. Is there a better approach?
@TeemoChan
@TeemoChan 10 місяців тому
I know there is a back button, but in general since the one in video is deprecated to dismiss a screen use @Environment (\.dismiss) var dismiss then in Button just write dismiss()
@Mike-er2ih
@Mike-er2ih 16 днів тому
Is it common and good practice in iOS Dev to pass viewModels around like that? Shouldn't RandomScreen (and each screen) basically have it's own ViewModel? And both ViewModels would access data from a common data storage which would be located lower in the general app architecture?
@douglasrutledge1325
@douglasrutledge1325 2 роки тому
this is beyond me, I'll have to start all over, or get 'boot' out. haha... I started back at Bootcamp #1 🤣
@rasheed1andrew
@rasheed1andrew 2 роки тому
towards the end, what if you wanted to pass data from 1 fruit in the array into the next screen.? instead of passing the whole array
@user-jp3vl6mv4w
@user-jp3vl6mv4w Рік тому
quick question. If I wanted to ensure that the data persists when that application is closed , would I need to save the information in coreData?
@armanwirawan7099
@armanwirawan7099 2 роки тому
hey Nick, since naviagtionBarItems is going to be deprecated do you ming making a video addressing that and how to go around it in the future thank you so mcuh !
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Hi Arman! Yea you're right. We should start using the .toolbar modifier instead of .navigationBarItems going forward. I've found that .navigationBarItems is still more stable for apps that support iOS 13/14 (so I'm still using it in production) but I will make a video for how to use .toolbar soon! I haven't done any iOS 15 features yet 😳
@armanwirawan7099
@armanwirawan7099 2 роки тому
@@SwiftfulThinking Is it normal that people keep using old versions of the Xcode and not update to the new one i feel like Xcode 14 is easier to use than xcode 15 becuase of the different nuances that apple decides to make this year
@diniezikry3121
@diniezikry3121 3 роки тому
Yo! So is it possible for the SecondView to update the information regarding the fruits (eg. changing the fruit count) in the first view?
@SwiftfulThinking
@SwiftfulThinking 3 роки тому
Yo!! Yes! That's the magic behind all of this. We are using the same ViewModel for all of the views. So if you update it in one place, it will automatically update everywhere. Because the data is @Published, it "published" the change to all of the views that are observing it!
@mithatucar1593
@mithatucar1593 2 роки тому
OMG thx so much.I have been struggling about how the fuck updating data works
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
😂🤣😂
@ShooMPozitiV
@ShooMPozitiV 2 роки тому
Nick, we all need your help about initialization of StateObjects. Please, can you explain about initialization of it? When my view getting parameters on init, which I need to initialize StateObject? What i have to do? I heard somewhere, that initialization of StateObject in init of view - is not good idea. But it's very often case. I'm looking information about it, but nothing ...
@ShooMPozitiV
@ShooMPozitiV Рік тому
Pleeeease (
@arrowfunction3200
@arrowfunction3200 8 місяців тому
where have you been all this time
@user-ou4un1rt9b
@user-ou4un1rt9b 7 місяців тому
Two years has passed from now and I just watched this video. Things have changed and I don't know if the reloaded items issue when onAppear method is called (28:34) it's still being a problem or we don't need to worry about anymore. If someone could answer my question would be useful
@SwiftfulThinking
@SwiftfulThinking 7 місяців тому
These property wrappers haven’t changed since the video was created. I’m not sure what your question is but I’m sure someone in our discord could help you out! discord.gg/bRPp9397aB
@sreekanthzyou
@sreekanthzyou 2 роки тому
Nice video Thanks for taking time and making these videos. Is there any example to checl When to use @StateObject and @ObservableObject. I want to check how @Observable object behaves when view is reloaded.
@SwiftfulThinking
@SwiftfulThinking 2 роки тому
Hey Sree, there's a very simple answer to that. Use @StateObject when you initialize the object (the first time you create it on a screen) and then use @ObservedObject if you need to pass it around the app after initializing it as a @StateObject.
@sreekanthzyou
@sreekanthzyou 2 роки тому
@@SwiftfulThinking Cool Nick. I go it. Thanks for Reply!
@hockeyandbeyond7566
@hockeyandbeyond7566 Рік тому
Great video, as all the others. But: isn't the rule of thumb to use @ObservableObject when you pass in your view model from the outside (i. e. you pass the view model into the view as a parameter) and to use @StateObject when you create the view model inside the view (as you do in the sample code)? So @ObservableObject var fruitViewModel = FruitViewModel() is actually a no-go?
@SwiftfulThinking
@SwiftfulThinking Рік тому
Yea that’s correct!
Їжа Закарпаття. Великий Гід.
1:00:29
Мiша Кацурiн
Переглядів 297 тис.
Артем Пивоваров х Klavdia Petrivna - Барабан
03:16
Artem Pivovarov
Переглядів 999 тис.
ЗРЯ Я 24 ЧАСА СТОЯЛ НА ГВОЗДЯХ! #нонале
00:35
@EnvironmentObject Explained in SwiftUI | iOS 17
21:16
AppStuff
Переглядів 3,6 тис.
How to use Generics in Swift | Advanced Learning #8
19:24
Swiftful Thinking
Переглядів 13 тис.
NavigationStack - SwiftUI Programmatic Navigation - iOS 16
20:20
Sean Allen
Переглядів 61 тис.
This Will Make Everyone Understand Golang Interfaces
21:03
Anthony GG
Переглядів 37 тис.
8 Common SwiftUI Mistakes - and how to fix them!
28:05
Paul Hudson
Переглядів 49 тис.
How to use inits and enums in SwiftUI | Bootcamp #13
19:30
Swiftful Thinking
Переглядів 35 тис.
Swift Dependency Injection - What is it? What are the benefits?
6:58
Їжа Закарпаття. Великий Гід.
1:00:29
Мiша Кацурiн
Переглядів 297 тис.