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

Поделиться
HTML-код
  • Опубликовано: 9 фев 2025
  • 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...
    🤙 WELCOME BACK 🤙
    WEBSITE: www.swiftful-t...
    DISCORD: / discord
    GITHUB: github.com/Swi...
    SAY THANKS: www.buymeacoff...

Комментарии • 200

  • @daraghmacgabhann1005
    @daraghmacgabhann1005 3 года назад +76

    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 года назад +1

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

  • @headach09
    @headach09 4 дня назад

    I’m amazed at how well you explained this, walking through a small project (that’s easy to digest) from start to finish with a clear demo. Thanks again!

  • @picabloo
    @picabloo 3 года назад +24

    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!

  • @Wiintb
    @Wiintb 2 года назад +17

    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.

  • @rajdhakate617
    @rajdhakate617 2 года назад +1

    Man really solved my doubt about @StateObject and @ObservedObject. Please make sure videos like these. It’s amazing

  • @filippokłosiewicz
    @filippokłosiewicz 11 месяцев назад +2

    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.

    • @Wallax92
      @Wallax92 5 месяцев назад

      I started with the Hacking with Swift course too, but when I read the challenge on Day 35 I wasn't confident enough to try complete them, so I started to look for another course & found this one. He explains things so well!

  • @nevemra5
    @nevemra5 Год назад +2

    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

  • @sols.2525
    @sols.2525 3 года назад +11

    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 года назад +2

      Wow! Thanks for the comment Scott. That's really appreciated. I'm very happy to help. Let me know if you have any questions!

  • @jlambert12013
    @jlambert12013 Год назад +1

    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!

  • @sundayreading
    @sundayreading 2 года назад

    Proud coffee member on a monthly basis now🥳. Please do not stop teaching!

  • @manishparihar3361
    @manishparihar3361 Год назад +1

    Finally i got clear concept of why, when and where we can use Identifiable, ObservaleObject, @StateObject, @ObservedObject. Thank you Nick

  • @ehsanbaghaki
    @ehsanbaghaki 2 года назад +1

    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!

  • @bbulliard
    @bbulliard 3 года назад +4

    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  3 года назад

      Haha that's a new one 😂 Glad you're enjoying the Bootcamp!

  • @bbulliard
    @bbulliard 3 года назад +1

    I love the way you explained the diff between @state and observable object. So abundantly clear

  • @coldy5845
    @coldy5845 6 месяцев назад

    Thank you Nick. Your videos are still being watched. Very very clear and thoughtful explanation of everything. Ofc it has to be watched maybe twice in order to absorb everything properly but such a video i can watch even 4 times! Keep up the good work!

  • @tamasterjek7520
    @tamasterjek7520 3 года назад +2

    First video I watched from you. Great explanation, easy to understand. Now I have to watch your other videos too :)

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад

      Welcome to the channel Tamas! I hope you like them :)

  • @AllanZhang16
    @AllanZhang16 Год назад +1

    What a great video. Perfect pacing, perfect explanations, and I like how you reinforce important concepts. Thanks again.

  • @solomonrajkumar5537
    @solomonrajkumar5537 2 года назад +1

    your boot camp is incredible awesome!!!

  • @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!

  • @ScaleCodingSkills
    @ScaleCodingSkills 2 года назад +1

    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.

  • @ConfidentlyRong-jo5yt
    @ConfidentlyRong-jo5yt 7 месяцев назад

    Can't thank you enough for demystifying the difference between @State & @StateObject , @ObservableObject & @ObservedObject and when to use each one respectively. Thanks bro! Also thanks for introducing me to the ProgressView() 😎

    • @SwiftfulThinking
      @SwiftfulThinking  7 месяцев назад +1

      You’re on #50 already? Beast mode 🚀🚀🚀

  • @wqoq
    @wqoq 3 года назад +2

    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 года назад +1

      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 3 года назад

      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.

  • @jameshawley2745
    @jameshawley2745 2 года назад +1

    Dude these tutorials are amazing! Keep up the good work!

  • @stepanivanov6877
    @stepanivanov6877 3 года назад +1

    Thank you Nick. You explained how to use @StateObject and @ObservedObject perfectly.

  • @basilson
    @basilson 3 года назад +4

    Thank you so much! I finally got it. Amazing video)

  • @rafaelclaycon
    @rafaelclaycon 2 года назад +1

    Omg, you gave me the missing piece I was looking for! Thank you so much! Great content!

  • @ataberkkilinctekin188
    @ataberkkilinctekin188 2 года назад

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

  • @maciusessa
    @maciusessa 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 🥳

  • @stefanlindholm3284
    @stefanlindholm3284 3 года назад +1

    This was very well explained! Thank you Nick!

  • @andresraigoza2082
    @andresraigoza2082 3 года назад +1

    Wonderful explanation!!! Thank you so much for investing your time making this videos.

  • @idrissben1613
    @idrissben1613 2 года назад

    thank you it's by far the best explanation i found for this subject!

  • @alanmaloney2791
    @alanmaloney2791 9 месяцев назад

    Brilliant video! Wonderfully clear and perfectly explained👍👍👍

  • @benceylan273
    @benceylan273 3 года назад +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  3 года назад +1

      Thanks for the comment Ben! Hearing that makes this all worth it for me. I'm glad this video helped 🥳

  • @mustafasabahy3092
    @mustafasabahy3092 2 года назад

    please don't stop you make life easier and more simple ♥

  • @bbulliard
    @bbulliard 2 года назад

    Wonderful for understanding state, stateobj and observable object. Thanks a bunch

  • @MarianaLaic
    @MarianaLaic Год назад

    Thank U! I am new on programming

  • @mamazur
    @mamazur 3 года назад

    All courses are amazing. You are the best.

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад

      Thank you for watching :)))

    • @mamazur
      @mamazur 3 года назад

      @@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.

  • @PengyGoy
    @PengyGoy 3 месяца назад

    Watching a ton of your videos - thank you!

  • @aloksinha2688
    @aloksinha2688 2 года назад

    Your explanation techniques is unique and simple..I love it. 👍

  • @aleksandertesenkov1421
    @aleksandertesenkov1421 Год назад

    Excellent video. Wonderful explanation. I knew most of this stuff but I learned many small details.

  • @いろい-b2l
    @いろい-b2l 2 года назад +3

    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 Год назад +1

      Do not do this. This will worsen the readability of the code.

    • @NICK4LIFE11
      @NICK4LIFE11 Год назад

      The type is inferred from the assignment. Saying it will worsen the readability is your opinion. @@redax3866

  • @olepetersen4460
    @olepetersen4460 3 года назад +3

    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.

  • @oglerma
    @oglerma 3 года назад

    Amazing explanation! Love all your videos. I was a little confused before and this kinda cleared everything up.

  • @tigran.zakaryan
    @tigran.zakaryan Год назад +1

    Great explanation!
    Thank you👍👍👍👍

  • @SwiftUIBootCampSachi
    @SwiftUIBootCampSachi 11 месяцев назад

    Thanks from the bottom of my Heart Mate ❤💚

  • @johnr.5475
    @johnr.5475 3 года назад +1

    Another excellent video. Thanks very much. It’s very well explained.

  • @muhammadusman-vc5re
    @muhammadusman-vc5re 2 года назад +4

    Not only describe the concept in a straightforward way but also tells the use cases of real projects. GOD bless on you.

  • @r_celikkaya
    @r_celikkaya 3 года назад

    I discovered you a few ago . I think that your videos is useful very .Thanks.

  • @farescherni6946
    @farescherni6946 3 года назад

    sir congratulations you have the best course covering swiftui

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад +1

      Wow, haha. I don't know if it's the best, but I'm very glad you're liking it! 🙃

  • @gregh528
    @gregh528 Год назад +1

    Great video. Thanks Nick.

  • @HiThereEdHere
    @HiThereEdHere 10 месяцев назад

    This video saved my life. Thank you.

  • @rmjalejandro
    @rmjalejandro Год назад

    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  Год назад

      I’m glad these videos helped! I do have a BuyMeACoffee: www.buymeacoffee.com/nicksarno
      Thanks 🍻

  • @valerieokelly6224
    @valerieokelly6224 2 года назад

    Top quality course 😍

  • @zcaur7329
    @zcaur7329 3 года назад

    What a well done video ! Kudos to you !

  • @Dragonboh1
    @Dragonboh1 Год назад

    keep doing what you doing man

  • @burakbozdoganli
    @burakbozdoganli Год назад

    Excellent lessons !

  • @maria_chemerys
    @maria_chemerys Год назад

    Thank you! You are our lifesaver❤

  • @heshamabdo6024
    @heshamabdo6024 2 года назад

    There are so many great ideas in this video
    thnk you

  • @douglasfers7
    @douglasfers7 Год назад

    Dud you're amazing! Thanks a lot!

  • @muqadasali8288
    @muqadasali8288 3 года назад

    Oh man you are a great teacher
    love from Pakistan

  • @adamkacha3823
    @adamkacha3823 2 года назад

    Great explanation. Thank you so much!

  • @dineshdas9983
    @dineshdas9983 2 года назад

    Sooo good explanation thanks mate🎉👍

  • @youngdomsey
    @youngdomsey 8 месяцев назад

    Great course! :)

  • @mig12394
    @mig12394 3 года назад

    Thanks a lot very clear with an exhaustif example

  • @shannonpaul8629
    @shannonpaul8629 Месяц назад

    Great video. and yes. it is a little confusing. but very informative.

  • @morreke
    @morreke 2 года назад

    Very well explained !!

  • @OttoFaerber
    @OttoFaerber 3 года назад

    Great, your teaching method is very understandable and helps me a lot, thanks a lot

  • @juich337
    @juich337 2 года назад

    The Best! Thanks!

  • @unknownerror999
    @unknownerror999 10 месяцев назад +1

    Nick, thank you so much for your lessons, do you have core data lessons? i did't find it

    • @SwiftfulThinking
      @SwiftfulThinking  10 месяцев назад

      Yes, they are in the Continued Learning playlist. You can also search “core” across all videos here: www.swiftful-thinking.com/search

  • @danielcovaci6930
    @danielcovaci6930 2 года назад

    great tutorial, keep up the good work

  • @MohamedAli-kg5im
    @MohamedAli-kg5im 2 года назад

    thanks man, fantastic video

  • @kauekaj
    @kauekaj Год назад

    Awesome! 👏

  • @Nunny25
    @Nunny25 3 года назад +1

    Great explanation, thanks

  • @ChristianPichardo-k5w
    @ChristianPichardo-k5w Год назад +1

    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?

  • @zhong2341
    @zhong2341 3 года назад

    Thank you so much for great explanations

  • @scottw1734
    @scottw1734 3 года назад

    Really well explained :)

  • @ado9678
    @ado9678 3 месяца назад

    @State = @StateObject : @Binding = @ObservedObject. Major difference is @StateObject & @ObservedObject are for instances of your class whereas @State & @Binding are more for your variables. Also, after doing some tinkering with the code I found that having a property with @ObservedObject will not reinitialize the object every time you step away and return to the screen as Nick had said, maybe it happens when you deal with more screens, not sure

  • @MikeyGio777
    @MikeyGio777 2 года назад

    Very Good!!!

  • @sema_f
    @sema_f 3 года назад

    We ❤️ to hang with u here! 🥳

  • @ostanjaffcompany173
    @ostanjaffcompany173 3 года назад

    Great video! Thanks.

  • @elliotknight5552
    @elliotknight5552 3 года назад

    My brain is burning lmao thanks for videos bro btw

  • @Raahim
    @Raahim 3 года назад

    very well explained!

  • @itmeocon25
    @itmeocon25 Год назад

    Thank you your video. How to use button delete instead onDelete() for List ?

  • @peterellison8306
    @peterellison8306 3 года назад +2

    Would it be possible to say something more about the "@Environment(\.presentationMode) var presentationMode" line? It is kind of a black box to me.

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад +1

      I will add a video on the @Environment to this playlist in the next few weeks. Thanks for the idea!

  • @diniezikry3121
    @diniezikry3121 3 года назад +2

    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 года назад +2

      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!

  • @Raahim
    @Raahim 3 года назад

    underrated

  • @ShooMPozitiV
    @ShooMPozitiV 3 года назад

    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 ...

  • @kurtschmucker4229
    @kurtschmucker4229 2 года назад +1

    During this video, I expected you to move the FruitViewModel class to its own file, but you didn't. Are you just delaying the "separate files" concept until a later video? A separate file seems like the logical organization for the code.

    • @SwiftfulThinking
      @SwiftfulThinking  2 года назад

      Yes, the ViewMode would normally have its own file. I just don’t always create separate files in the videos because it’s easier to organize / display in the tutorial. Moving that code to another file wouldn’t change the actual implementation though!

  • @eddydon1929
    @eddydon1929 3 года назад +1

    can you make a video on firebase or Vapor

  • @jalapisco
    @jalapisco 2 года назад

    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  2 года назад

      Hey! This is not a beginner topic, but everything you need to know about structs vs classes is here: ruclips.net/video/-JLenSTKEcA/видео.html

    • @jalapisco
      @jalapisco 2 года назад

      @@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  2 года назад

      @@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.

  • @Mike-er2ih
    @Mike-er2ih 10 месяцев назад

    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?

  • @dugrut1325
    @dugrut1325 2 года назад

    this is beyond me, I'll have to start all over, or get 'boot' out. haha... I started back at Bootcamp #1 🤣

  • @udesai2011
    @udesai2011 3 года назад

    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?

  • @justmytraining4190
    @justmytraining4190 7 месяцев назад

    09:58 its magic for me:)

  • @M4CARBINE556
    @M4CARBINE556 Год назад

    Doesn’t using @StateObject prevent the data from updating? What if a different user added a new item to the db, how would this user see it if the data never fetches again?

  • @armanwirawan7099
    @armanwirawan7099 3 года назад

    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  3 года назад

      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 3 года назад

      @@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

  • @PengDeng-e6r
    @PengDeng-e6r 7 месяцев назад

    hey, i change @ObservedObject to @StateObject in RandomScreen, it works as well, so i really confused.

  • @LuisGonzalez-m5f8o
    @LuisGonzalez-m5f8o Год назад

    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  Год назад

      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

  • @perrinmatias
    @perrinmatias 3 года назад

    Hi Nick, It is possible to do the same, but instead of a list, make 8 buttons that each go to a different view? thanks bye!

    • @SwiftfulThinking
      @SwiftfulThinking  3 года назад

      Hi Matias! Yes, anything is possible :) You just need to change the "destination" View inside the NavigationLinks

  • @williamwildsmith5162
    @williamwildsmith5162 2 года назад

    Quick question Nick...
    Why have both a FruitModel struct, and a FruitViewModel class?
    Why not just have a FruitModel class?
    Sorry, if it's a dumb question!
    Thank you