Swift - Class vs. Struct Explained

Поделиться
HTML-код
  • Опубликовано: 5 фев 2025

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

  • @mario_luis_dev
    @mario_luis_dev Год назад +4

    you’re really crushing it with these videos Sean. Not only the clear way you teach things, but also the high quality of the videos with all the animations / transitions and such. Masterful 👏

  • @manuelapenuela7545
    @manuelapenuela7545 11 месяцев назад +8

    This is better than my university lectures, thank you!!

    • @seanallen
      @seanallen  10 месяцев назад +2

      Thanks for the kind words :)

  • @nX-
    @nX- 2 года назад +14

    Good simple explanation! Just one small detail (but it is just nit picking), is that at 3:40, the struct is not instantly copied once you assign it to a new variable. It is only copied once you mutate the new variable, since structs in Swift use CoW (Copy-on-Write) semantics.

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

      yep, but no one cares :D

    • @nX-
      @nX- 2 года назад +5

      @@Stricken174 This is actually asked in some interviews ;) Also, it can be important to know when dealing with performance issues. But ofc if you are just learning these concepts as a beginner, it is not important at all.

    • @seanallen
      @seanallen  2 года назад +8

      Agreed. These videos are meant for those studying for their first Jr dev job. You’re not gonna fail a Jr dev interview if you don’t explain CoW semantics. There’s a balance in teaching of not going too deep into the weeds that you overwhelm the learner.

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

      @@nX- Thats true, but some of interviewers have no idea about cow) Good to know it for yourself.

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

      The bigger error is that he claims structs are objects.

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

    As a practicing IOS developer I’m aware of class / struct differences, yet your explanation is the best I’ve ever encountered 🎉🎉🎉

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

    you know I have learned today! not everyone is meant to be a teacher seriously, same topic same code different explanation. With these guy I understood the difference in less than 5min you're the best

  • @chevlim
    @chevlim 8 месяцев назад +2

    You explained this so well! Was learning about this in Swiftful Thinking’s video playlist but had trouble understand. Your analogy is a gem!

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

      Glad it was helpful!

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

    Great video! From a simple point of view in swift is “safe” to assume you can use struct by default unless you need class specific features. Structs seem like class-lite

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

    Also classes have initializers/deinitializers due allocation on the heap

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

    this explanation was the best sean. Thank you very much

  • @MachineLearning-rw7jd
    @MachineLearning-rw7jd Год назад

    Your analogy would be great if you had mentioned that you were not using Excel with cloud features. Since you have not, you made it look like Excel is stuck in time.

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

    New to Swift, but 50 years coding experience. Did you mean to say that a Class Instance is by-ref rather than a Class?

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

    Could you please add “Closures” to your Swift course? I’m currently doing your SwiftUI course and I’m having a wonderful time! You explain the material better than my Lecturers at University. Thank you

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

      Glad you’re enjoying them! I did a recent video about closures here - Swift Closures Explained
      ruclips.net/video/ND44vQ5iJyc/видео.html

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

    A reference type lets the calling code store the input value remotely in the class. Value type is a struct that lets the calling code block store the input value locally inside itself.

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

    why do we use struck instead of class can you elaborate it further

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

    Another great video with analogy. One question I have though, does it matter, when creating a struct, the properties are declared as ‘let’ or ‘var’. I am sure there is a difference but what is it is hard for me to picture. Any hint you can point to?

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

      Exclamer I'm not a swift developer I just threw up the wiki and used my knowledge of other languages.
      If you have a struct:
      struct Point {
      var x: int
      let y: int
      }
      And an "instance" :
      var p = Point(x: 0, y: 0)
      p.x = 5 //works p.x is now 5
      p.y = 4 //error p.y is immutable
      If you declared p with let:
      let p2 = Point(x: 0, y: 0)
      p.x = 5 //error p is immutable
      p.y = 4 //error p is immutable
      In short the higher level var/let decides for all underlying fields. Imagine it like a tree where every let is a dead end and var is another emerging branch.
      Hope this helps and is somewhat correct.

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

    52sec too late notification.
    Great content, I enjoy the way you teach.

  • @paulcristo
    @paulcristo 11 месяцев назад +1

    Very informative. Thanks.

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

    Hi Sean can you please make a tutorial on “ State Machine Design in Swift” ? Please please

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

    Thank You mate ! really helpful video

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

      Glad it helped

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

      Good series on the way to Junior IOS dev and to sum up before interviews

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

      Best of luck on the interviews!

  • @PrestonWeight
    @PrestonWeight 2 года назад +2

    Great stuff!

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

      Glad you enjoyed it, Preston!

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

    Bro you explain things in a way that I actually understand. I’m learning this from scratch with no coding experience, so trying to learn the why behind why things are coded a certain way and the functions has been a challenge.

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

    Google sheet example was nice

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

    The most interesting bit you unfortunately didn't cover. What happens if the properties of a struct are not primitive types? How does that influence the reference vs value situation? That's where it gets a little more tricky to reason about. Otherwise a nice explanation.

  • @VladimirKim-cl3rh
    @VladimirKim-cl3rh Год назад

    Bro Struct in Swift not a "pure value type", if you don't know - it's a "value semantic type", and proceed using the struct sometimes you may get unusual behavior related storage in heap or stack memory allocation

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

    beautfiul explanation liked and subscribed

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

    Struct is to create Type which is not same as Class which create Object

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

    What about actor

  • @stesch-f
    @stesch-f Год назад

    I'm still astounded that arrays and dictionaries are structs.

  • @mussadiqhafeez9323
    @mussadiqhafeez9323 11 месяцев назад +1

    very helpful

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

      Glad it helped

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

    clear explanations!!!

  • @radfaredu644
    @radfaredu644 2 года назад +2

    Tank you !

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

    Great explain

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

    How do structs behave as part of swiftui observable objects? I bumped into some weird behaviors when relying on observable objects having nested structures in them. Textfields with bindings & cursor jumping to very end of text when editing in the middle of a field namely. Another video on this maybe? Thanks for well made videos - I’ve learned quite a bit from them!

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

      There are some issues / features when it comes to when views are redrawn, it gives greater control when done right.

  • @Scott-yq5sx
    @Scott-yq5sx Год назад

    Thanks!

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

    so struct is just like a structure. like a school who have their preset ( lessons teachers infrastructure) unchange, while class is the class itself, moving towards grade, to the next, inheriting previous knowledge acquired

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

      Congratulations on making a simple topic more complex with this explanation 😂.
      But then if it somehow works for you, more power to ya.

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

      Your analogy nested classes inside structs. Making a class a property or feature of a struct.

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

    super helpfull

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

    Bro please I just purchased your SwiftUI Mega Bundle. But can you please recommend me a Mac machine to buy for the course please

    • @bobweiram6321
      @bobweiram6321 2 года назад +4

      What's your budget? Right now you can buy a Mac M2 mini for about $500. This is the best deal going if you're OK with being chained to a desk. If you want something portable, an M1 or M2 MacBook Air is a fantastic buy. Alternatively, you can buy a used M1 MacBook Pro.

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

      @@bobweiram6321 am planning to buy MacBook Pro 13 m2 chip

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

      I would suggest any apple silicon Mac mini. Extremely powerful for a frankly ridiculously low price. If u want portable get a used 13” M1 or m2 MBP for battery life and fans

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

      @@Daystarjn if you want a MacBook buy m2 air or 14“ pro the 13“ is just a more expensive air with old design. Or you could go for a cheap m1 air.

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

    in the swift UI example, it reads "struct LoadingView: view"... isnt that inheritance? great video Sean!

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

      I see where that could be confusing, but in the case of a struct you are conforming to a protocol when you do that. So "LoadingView" is conforming to the "View" protocol.

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

      @@seanallen super clear. thanks sean!

  • @chrispy104k
    @chrispy104k 2 года назад +2

    Your explanations are spot on Sean.

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

      I appreciate it, Chrispy!

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

    Not sure why my comment was deleted, but I said Structs being light weight doesn't make sense since they are passed by value. There's a lot of coping, creating and destroying of data. That is performance heavy.

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

      I didn't delete any comment. And in my comments backend, I see the comment right below this one (would provide a screenshot if I could)

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

      @@seanallen I think it was RUclips delaying my comment for some reason. I saw it pop-up.

  • @王登科-u5c
    @王登科-u5c Год назад

    I absolutely loved watching Swift - Class vs. Struct Explained by Sean Allen on RUclips! As someone who is starting out in iOS development, this video was incredibly helpful in explaining the differences between classes and structs. The way Sean explained the concept of reference types and value types was easy to understand, and the visuals he used to illustrate his points were very effective.
    I want to thank Sean for making this video and for sharing his knowledge with the iOS development community. His clear and concise explanations make even the most complex concepts easy to follow. I also appreciate the discount code he provided for Squarespace - I'm definitely going to check it out!
    Overall, I highly recommend this video to anyone who's interested in iOS development or is preparing for an interview. Sean's expertise and teaching style make this video a must-watch for anyone who wants to improve their understanding of Swift programming. Thank you, Sean!

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

    Structs being light weight because they're passed by value doesn't make sense. In many languages reference types are light weight, since only one instance of that thing exists. Creating and Destroying objects has a severe performance cost. Value types are also copied. In your SwiftUI example it looks like Structs also have inheritance. They inherit from View.

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

      Nah they don’t inherit from View, they conform to the View protocol

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

    nice

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

    Yeah…sticking with C…

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

    xxxxx😂❤😂😂😂

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

    You also can use inheritance in struct. But their inheritance does work only for protocols. So you can't say there is no inheritance in struct. 😉

    • @noamif
      @noamif 2 года назад +2

      That’s not inheritance, that’s protocol implementation/ compliance