All right. 1
So in this lesson, I want to talk about Swift Structures versus Swift Classes. 2
Now, it's really important to state that I'm talking about Swift Structures in particular, because even 3
though different programming languages, a lot of them will have these things called struct. 4
They have different capabilities. 5
And from language to language, they actually behave quite differently. 6
So it's important to realize that we're just talking about the structures that are in the Swift Programming 7
Language. Now, to create a structure, 8
it's pretty simple. 9
You would create it just as you would any class and you simply replace the keyword class with struct. 10
Now, the naming convention for structures are, again, the same as classes. 11
You will start off with a capital letter to name your structures which is different from how you would 12
camel case your property names or your function names. 13
Now, structures in Swift can have properties as well as methods, and they can do almost everything that 14
classes can, but they work a little bit differently. 15
Now, a lot of times whenever I give this talk, students will ask, "Well, which one is better, struct or classes? 16
Which one comes out on top? 17
And this is very similar to another question that I often get which is which is the best programming 18
language to learn? 19
And they both have the same problem. 20
There is no such thing as one is better than the other, but you should see everything inside programming, 21
be it a programming language or a component being tools in your tool belt. and you should choose them 22
in different scenarios and for different purposes. I'm just saying, you know, which is better struct versus 23
classes. It's kind of saying like which is better, a horse or a mule, 24
right? 25
They both have their uses. 26
They both have their pros and cons. Now, similar to a mule, 27
a struct is infertile. 28
So just as you can't mate a female and a male mule to create a baby mule, you also can't inherit from struts 29
or try to subclass in a struct. 30
Inheritance is reserved for classes and this is one of the biggest differences between structs and classes 31
and when you would use each, whenever you need inheritance. 32
So if you're working with object-oriented programming, we know how important inheritance is. 33
So far, none of our user interface components have been created from scratch by us. 34
We've always been inheriting UIView or UIViewController or UITableViewController, and this 35
capability is not given to structs. 36
Now, the other major difference between structs and classes is that they live in different places in memory. 37
So in computers and that includes handheld computers, such as the iPhone, there's two main ways of storing 38
data for random access. 39
So this is referring to the RAM or the part of the memory that needs to be frequently accessed and it's 40
not the same as your permanent storage, such as your SSD or your hard drive. And stacks and heaps are 41
simply different ways of organizing the RAM and they're used for different purposes. 42
So let's say that you're working at an office, a stack might just be that a stack of papers which you 43
have to get through from top to bottom, but a heap could be just a random heap of all of your files and 44
folders. 45
Now, structs live on the stack. 46
And the thing about the stack is that it follows something called a first in, last out system. 47
So that means that the first piece of data that went into the stack will be the last one that pops off 48
the stack, 49
and this means that the most recently saved pieces of data will be on hand and quickly accessed when 50
you need it. 51
Now, classes on the other hand, they live in the heap. 52
And if we create a new class that's called MyClass with a property and a method, 53
then when we initialize our class, let's call it MyClass. 54
Then all of that data gets allocated some memory and it will be saved somewhere in the heap. 55
Now, in order to find it in the heap, our class also has a reference in the stack. 56
So if our class was called MyClass, then that reference MyClass will be put onto the stack, and that 57
reference will point towards our allocated block of data in the heap, so that when we need to access 58
something inside this object MyClass, will be able to locate it using the reference MyClass. 59
Now, another common thing that you'll hear when people are discussing struct versus classes is that struct 60
of value types. 61
And this means that they stole the actual data values. 62
Now, classes are reference types and we store a reference to a block of memory, and the reference is just 63
an instruction for how you might locate that block in the heap. 64
Now, what's the difference between value and reference types? 65
Well, if I was to copy a struct or a value type in this case, then it would create a new copy that has 66
exactly the same values. 67
But if I was to copy a reference type, so a class, then I would copy only the instructions for how to 68
locate the block of memory inside the heap. 69
So this is a really important concept, 70
value types and reference types. 71
And so in order to understand it better, 72
let's head over to playgrounds and do a deep dive into how they work and behave in real life when we're 73
writing code for structs and classes.