1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Using Advanced Techniques in Swift

Struct Overview


Advanced Swift
Struct Overview
7m 23s
Guard Let
5m 52s

The course is part of this learning path

Struct Overview

This course focuses on advanced techniques in Swift. You'll be introduced to structs and how we can use them in our code.


Hi. Within this lecture, we're going to see what a struct is and why do we have to use them in our projects, and why do we choose them over classes sometime. So, as you can see, it's a class. It's similar to class, but it's very different technically. So, I'm going to tell you all about these differences, and I'm going to tell you about the theory of the structs first. And then later on, we're going to see them in practice so that we will have a complete understanding of structs versus classes. So, structs and classes are similar, but they have their disadvantages and advantages. First of all, the biggest disadvantage of the struct is the inheritance. So, structs do not have inheritance while classes have. So, remember the intermediate swift section, we used classes to create models, we used classes to create a musician model, for example. And we created a super musician which inherited the methods and attributes of the musician class. And we could have used all these methods and attributes inside of the super musician as well. We cannot do that in structs. So, this is one of the biggest differences, and I believe this is the only disadvantage of the struct. In fact, Apple suggests to use struct most of the time. And if you ever going to work with inheritance, if you ever need inheritance, then you just switch to the classes. So, Apple suggests using structs over classes. Why is that? Because structs are actually faster and simpler. And the main technique reason for that is they run on stacks rather than heap. So, maybe you have heard these terms before, they're memory management, storage management objects or tools residing inside of RAM. So, if you come over here and just search for heap versus stack in Google, you will have like millions of results. So, you can just go along with any of these results and see for yourself. So, we use stack and heap for memory allocation, for memory management. And you will see a lot of explanations in C programming language, because this is actually related to programming and computer science. So, when the subject is that basic, when the subject is that essential, people tend to teach, people tend to explain things in C programming language. Of course, we don't have to understand C in order to understand stack and heap memory allocation. We have to understand how they work. So, they actually store some memory for us. They actually store some data. They actually store some values for us so that we can go and take that value from RAM. And RAM is used for short-term memory as you might know. And in our iPhones and in our iPads, of course, we have RAM and we use that memory allocation tools. So, heap works on FIFO; first-in, first-out principle. On the other hand, stack works on FILO; first-in, last-out principle. So, whatever value start in stack first, it gets out last. So, this gives us faster and simpler operations. And in classes, since we use heap, we use reference types. So, when we create an object with our classes, we actually create a reference to that object inside of the heap. But in structs, we don't create references, we create values. We create another object for example. When we copy an object inside of a class, it means that we create another reference. When we copy an object inside of a struct, it means that we are actually copying and creating another object. So, this gives us faster and simpler operations in struct. Of course, you cannot understand right now because you have to see this in practice and you will see in the next lecture, don't worry. The last thing you may want to know about struct, they give us the opportunity to work with true immutability, whereas in classes we don't have that feature. And we will see that in action as well. Everything I'm telling you right now will be put in action during this section so that you will understand it much better. I'm just trying to compare the class and struck right now so that you will understand they are similar but they have some disadvantages and advantages over each other. The final advantage of the class is that they work with objective-C while we cannot work with objective-C in the struct. But I believe that's not a very big disadvantage, because we generally use Swift right now in iOS Development. However, I believe only disadvantage of the struct is the inheritance. Let's see all of this in action so you will understand it in a much better way.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics