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