Swift - Class vs. Struct Explained

Поделиться
HTML-код
  • Опубликовано: 4 июл 2024
  • Go to squarespace.com/seanallen to save 10% off your first purchase of a website or domain using code SEANALLEN.
    Classes vs. Structs is a classic iOS Developer Interview question. In this video I explain the difference between a reference type and a value type as well as when to use a struct or a class. This tutorial was created using Xcode 14 and iOS 16.
    iOS Developer Interview Questions Playlist:
    • Swift Interview Tips
    My iOS Dev Courses:
    seanallen.teachable.com/
    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
    Ray Wenderlich Books:
    store.raywenderlich.com/a/208...
    #swift #softwaredeveloper #iosdeveloper
    Timestamps:
    0:00 - iOS Interview Question
    0:21 - Short Answer - Reference vs. Value Types
    0:48 - Class - Reference Type
    2:35 - Struct - Value Type
    4:18 - When to use Class or Struct
  • НаукаНаука

Комментарии • 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 👏

  • @chevlim
    @chevlim Месяц назад +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  Месяц назад

      Glad it was helpful!

  • @dmitryarkharov2525
    @dmitryarkharov2525 Год назад +3

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

  • @nX-
    @nX- Год назад +11

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

      yep, but no one cares :D

    • @nX-
      @nX- Год назад +3

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

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

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

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

      The bigger error is that he claims structs are objects.

  • @nmyster
    @nmyster 15 дней назад

    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

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

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

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

    • @seanallen
      @seanallen  3 месяца назад +1

      Thanks for the kind words :)

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

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

  • @ndrukz
    @ndrukz 5 месяцев назад +1

    this explanation was the best sean. Thank you very much

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

      You're very welcome!

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

    Very informative. Thanks.

  • @kambala1199
    @kambala1199 9 месяцев назад +1

    Also classes have initializers/deinitializers due allocation on the heap

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

    Great stuff!

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

      Glad you enjoyed it, Preston!

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

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

  • @tcurdt
    @tcurdt Год назад +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.

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

    clear explanations!!!

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

    Great explain

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

    Google sheet example was nice

  • @nileshjdarji
    @nileshjdarji Год назад +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

      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.

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

    Thanks!

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

    Tank you !

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

    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!

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

    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

  • @fatihsahin6863
    @fatihsahin6863 6 месяцев назад +1

    beautfiul explanation liked and subscribed

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

      Glad you liked it!

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

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

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

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

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

    very helpful

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

    super helpfull

  • @MachineLearning-rw7jd
    @MachineLearning-rw7jd 9 месяцев назад

    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.

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

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

  • @VladimirKim-cl3rh
    @VladimirKim-cl3rh 9 месяцев назад

    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

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

    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.

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

    nice

  • @nickdhrones6425
    @nickdhrones6425 8 месяцев назад +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?

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

    What about actor

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

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

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

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

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

    • @tompov227
      @tompov227 Год назад +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 Год назад +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.

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

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

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

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

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

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

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

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

    • @seanallen
      @seanallen  Год назад +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 Год назад

      @@seanallen super clear. thanks sean!

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

    Your explanations are spot on Sean.

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

      I appreciate it, Chrispy!

  • @wlcrutch
    @wlcrutch 12 дней назад

    Yeah…sticking with C…

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

    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  6 месяцев назад

      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 6 месяцев назад +1

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

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

    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 3 месяца назад

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

  • @user-jz7bw9cm3n
    @user-jz7bw9cm3n Год назад

    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!

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

    xxxxx😂❤😂😂😂

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

    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

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