Swift - Class vs. Struct Explained

preview_player
Показать описание

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:

My iOS Dev Courses:

Twitter:

Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):

Paul Hudson's Hacking With Swift:

Donny Wals - Combine:

Mark Moeyken’s SwiftUI Books:

Ray Wenderlich Books:

#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
Рекомендации по теме
Комментарии
Автор

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 👏

mario_luis_dev
Автор

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

dmitryarkharov
Автор

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

chevlim
Автор

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

nmyster
Автор

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.

nX-
Автор

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

giantdworf
Автор

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

manuelapenuela
Автор

52sec too late notification.

Great content, I enjoy the way you teach.

Ikona
Автор

this explanation was the best sean. Thank you very much

ndrukz
Автор

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

AmitBiswastunebox
Автор

Also classes have initializers/deinitializers due allocation on the heap

kambala
Автор

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.

tcurdt
Автор

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

Corazon
Автор

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.

ingongoyama
Автор

beautfiul explanation liked and subscribed

fatihsahin
Автор

Thank You mate ! really helpful video

borysg
Автор

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.

MachineLearning-rwjd
Автор

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!

tapiokuusisto
Автор

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?

nileshjdarji
Автор

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

VladimirKim-clrh