Swift - Class vs. Struct Explained

  Переглядів 28,419

Sean Allen

Sean Allen

День тому

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

КОМЕНТАРІ: 75
@mario_luis_dev
@mario_luis_dev 10 місяців тому
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 🎉🎉🎉
@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.
@giantdworf2026
@giantdworf2026 11 місяців тому
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 Місяць тому
This is better than my university lectures, thank you!!
@seanallen
@seanallen Місяць тому
Thanks for the kind words :)
@Ikona731
@Ikona731 Рік тому
52sec too late notification. Great content, I enjoy the way you teach.
@ndrukz
@ndrukz 3 місяці тому
this explanation was the best sean. Thank you very much
@seanallen
@seanallen 3 місяці тому
You're very welcome!
@paulcristo
@paulcristo 2 місяці тому
Very informative. Thanks.
@tszkinwong6170
@tszkinwong6170 10 місяців тому
clear explanations!!!
@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!
@kadeus
@kadeus Рік тому
Great explain
@kambala1199
@kambala1199 7 місяців тому
Also classes have initializers/deinitializers due allocation on the heap
@PrestonWeight
@PrestonWeight Рік тому
Great stuff!
@seanallen
@seanallen Рік тому
Glad you enjoyed it, Preston!
@asherazeem3282
@asherazeem3282 Рік тому
Google sheet example was nice
@Scott-yq5sx
@Scott-yq5sx 10 місяців тому
Thanks!
@radfaredu644
@radfaredu644 Рік тому
Tank you !
@fatihsahin6863
@fatihsahin6863 4 місяці тому
beautfiul explanation liked and subscribed
@seanallen
@seanallen 4 місяці тому
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.
@AmitBiswastunebox
@AmitBiswastunebox Рік тому
Hi Sean can you please make a tutorial on “ State Machine Design in Swift” ? Please please
@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.
@mussadiqhafeez9323
@mussadiqhafeez9323 2 місяці тому
very helpful
@seanallen
@seanallen Місяць тому
Glad it helped
@swiftui_and_swift
@swiftui_and_swift 11 місяців тому
super helpfull
@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 ukposts.info/have/v-deo/hnRlaKyIbJ972pc.html
@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.
@MachineLearning-rw7jd
@MachineLearning-rw7jd 7 місяців тому
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.
@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.
@VladimirKim-cl3rh
@VladimirKim-cl3rh 7 місяців тому
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
@victorriurean
@victorriurean Рік тому
nice
@umerakram2645
@umerakram2645 25 днів тому
why do we use struck instead of class can you elaborate it further
@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.
@stesch-f
@stesch-f 10 місяців тому
I'm still astounded that arrays and dictionaries are structs.
@nickdhrones6425
@nickdhrones6425 6 місяців тому
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 Місяць тому
yes
@jeanchindeko5477
@jeanchindeko5477 6 місяців тому
Struct is to create Type which is not same as Class which create Object
@CodingWithTaaxo
@CodingWithTaaxo 7 місяців тому
What about actor
@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.
@chudchadanstud
@chudchadanstud 4 місяці тому
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 4 місяці тому
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 4 місяці тому
@@seanallen I think it was UKposts delaying my comment for some reason. I saw it pop-up.
@chrispy104k
@chrispy104k Рік тому
Your explanations are spot on Sean.
@seanallen
@seanallen Рік тому
I appreciate it, Chrispy!
@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!
@Relllz521
@Relllz521 9 місяців тому
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.
@chudchadanstud
@chudchadanstud 4 місяці тому
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 Місяць тому
Nah they don’t inherit from View, they conform to the View protocol
@user-jz7bw9cm3n
@user-jz7bw9cm3n 10 місяців тому
I absolutely loved watching Swift - Class vs. Struct Explained by Sean Allen on UKposts! 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!
@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
@tsunningwah3471
@tsunningwah3471 2 місяці тому
xxxxx😂❤😂😂😂
Swift Closures Explained
14:23
Sean Allen
Переглядів 56 тис.
Swift Optionals - How to Unwrap (real examples)
14:20
Sean Allen
Переглядів 10 тис.
Анита просто на химии, поэтому такая сильная
00:21
Женя Лизогуб SHORTS
Переглядів 2,8 млн
How to use Lazy in Swift
9:56
Sean Allen
Переглядів 38 тис.
Python vs Swift | Chris Lattner and Lex Fridman
11:16
Lex Clips
Переглядів 146 тис.
Swift 5.9 Released | Prime Reacts
14:36
ThePrimeTime
Переглядів 59 тис.
Swift Struct vs. Class Explained (iOS Interview Question)
5:33
Logan Koshenka
Переглядів 3,6 тис.
Swift Algorithms - Faster, Cleaner Code. (Chunked Example)
13:48
Sean Allen
Переглядів 16 тис.
iOS Concurrency and Threading - iOS Interview Question - Swift
7:50
Sean Allen
Переглядів 104 тис.
Protocol-Oriented Programming in Swift / WWDC15 / Session 408
45:52
WWDC sessions
Переглядів 8 тис.
UIViewController - Lifecycle | Swift | viewDidLoad
7:57
Sean Allen
Переглядів 39 тис.
🤯Самая КРУТАЯ Функция #shorts
0:58
YOLODROID
Переглядів 2,7 млн
Лучший Смартфон До 149 Баксов!!!??? itel s24
20:25
РасПаковка ДваПаковка
Переглядів 35 тис.
Нужно ли чистить ПК от пыли?
0:59
CompShop Shorts
Переглядів 64 тис.