Swift - Class vs. Struct Explained

  Рет қаралды 34,068

Sean Allen

Sean Allen

Күн бұрын

Пікірлер: 79
@nX-
@nX- Жыл бұрын
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- Жыл бұрын
@@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 Жыл бұрын
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.
@mario_luis_dev
@mario_luis_dev Жыл бұрын
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 👏
@dmitryarkharov2525
@dmitryarkharov2525 Жыл бұрын
As a practicing IOS developer I’m aware of class / struct differences, yet your explanation is the best I’ve ever encountered 🎉🎉🎉
@chevlim
@chevlim 4 ай бұрын
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 4 ай бұрын
Glad it was helpful!
@manuelapenuela7545
@manuelapenuela7545 7 ай бұрын
This is better than my university lectures, thank you!!
@seanallen
@seanallen 7 ай бұрын
Thanks for the kind words :)
@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
@nmyster
@nmyster 4 ай бұрын
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
@ndrukz
@ndrukz 9 ай бұрын
this explanation was the best sean. Thank you very much
@seanallen
@seanallen 9 ай бұрын
You're very welcome!
@nileshjdarji
@nileshjdarji Жыл бұрын
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 Жыл бұрын
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.
@nickdhrones6425
@nickdhrones6425 Жыл бұрын
New to Swift, but 50 years coding experience. Did you mean to say that a Class Instance is by-ref rather than a Class?
@Dfso200x
@Dfso200x 6 ай бұрын
yes
@Corazon15
@Corazon15 Жыл бұрын
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 Жыл бұрын
Glad you’re enjoying them! I did a recent video about closures here - Swift Closures Explained kzbin.info/www/bejne/hHWXZamHas59r8U
@tcurdt
@tcurdt Жыл бұрын
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.
@paulcristo
@paulcristo 8 ай бұрын
Very informative. Thanks.
@borysg9999
@borysg9999 Жыл бұрын
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 Жыл бұрын
Best of luck on the interviews!
@AmitBiswastunebox
@AmitBiswastunebox Жыл бұрын
Hi Sean can you please make a tutorial on “ State Machine Design in Swift” ? Please please
@kambala1199
@kambala1199 Жыл бұрын
Also classes have initializers/deinitializers due allocation on the heap
@PrestonWeight
@PrestonWeight Жыл бұрын
Great stuff!
@seanallen
@seanallen Жыл бұрын
Glad you enjoyed it, Preston!
@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.
@fatihsahin6863
@fatihsahin6863 10 ай бұрын
beautfiul explanation liked and subscribed
@seanallen
@seanallen 9 ай бұрын
Glad you liked it!
@Ikona731
@Ikona731 Жыл бұрын
52sec too late notification. Great content, I enjoy the way you teach.
@umerakram2645
@umerakram2645 6 ай бұрын
why do we use struck instead of class can you elaborate it further
@asherazeem3282
@asherazeem3282 Жыл бұрын
Google sheet example was nice
@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.
@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
@chrispy104k
@chrispy104k Жыл бұрын
Your explanations are spot on Sean.
@seanallen
@seanallen Жыл бұрын
I appreciate it, Chrispy!
@mussadiqhafeez9323
@mussadiqhafeez9323 7 ай бұрын
very helpful
@seanallen
@seanallen 7 ай бұрын
Glad it helped
@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.
@kadeus
@kadeus Жыл бұрын
Great explain
@tszkinwong6170
@tszkinwong6170 Жыл бұрын
clear explanations!!!
@radfaredu644
@radfaredu644 Жыл бұрын
Tank you !
@Scott-yq5sx
@Scott-yq5sx Жыл бұрын
Thanks!
@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.
@jeanchindeko5477
@jeanchindeko5477 Жыл бұрын
Struct is to create Type which is not same as Class which create Object
@stesch-f
@stesch-f Жыл бұрын
I'm still astounded that arrays and dictionaries are structs.
@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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
@@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.
@swiftui_and_swift
@swiftui_and_swift Жыл бұрын
super helpfull
@CodingWithTaaxo
@CodingWithTaaxo Жыл бұрын
What about actor
@Ni7ram
@Ni7ram Жыл бұрын
in the swift UI example, it reads "struct LoadingView: view"... isnt that inheritance? great video Sean!
@seanallen
@seanallen Жыл бұрын
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!
@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.
@chudchadanstud
@chudchadanstud 10 ай бұрын
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 9 ай бұрын
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 9 ай бұрын
@@seanallen I think it was KZbin delaying my comment for some reason. I saw it pop-up.
@victorriurean
@victorriurean Жыл бұрын
nice
@chudchadanstud
@chudchadanstud 10 ай бұрын
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 7 ай бұрын
Nah they don’t inherit from View, they conform to the View protocol
@wlcrutch
@wlcrutch 3 ай бұрын
Yeah…sticking with C…
@王登科-u5c
@王登科-u5c Жыл бұрын
I absolutely loved watching Swift - Class vs. Struct Explained by Sean Allen on KZbin! 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 7 ай бұрын
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 Жыл бұрын
That’s not inheritance, that’s protocol implementation/ compliance
Swift Closures Explained
14:23
Sean Allen
Рет қаралды 66 М.
Smart Sigma Kid #funny #sigma
00:14
CRAZY GREAPA
Рет қаралды 11 МЛН
🕊️Valera🕊️
00:34
DO$HIK
Рет қаралды 7 МЛН
Swift API Calls for Beginners (Networking) - Async Await & JSON
25:35
Swift Generics for Beginners - Eliminate Code Duplication
10:37
Sean Allen
Рет қаралды 20 М.
Swift 5.9 Released | Prime Reacts
14:36
ThePrimeTime
Рет қаралды 70 М.
2-Minute Rule to Learn Coding - Atomic Habits
7:58
Sahil & Sarra
Рет қаралды 1,1 МЛН
Class vs Struct | C++ (What's the Difference?)
9:25
Caleb Curry
Рет қаралды 12 М.
C++ vs Rust: which is faster?
21:15
fasterthanlime
Рет қаралды 398 М.
Python vs Swift | Chris Lattner and Lex Fridman
11:16
Lex Clips
Рет қаралды 154 М.
Python vs Swift | Prime Reacts
19:44
ThePrimeTime
Рет қаралды 78 М.