Swift has quite a few ways of helping you structure your data.
Swift has quite a few ways of helping you structure your data. In this module you’ll learn about Swift Structs, first with a review of the struct syntax and organization. Then you’ll learn about the difference between value types and reference types. Structs are value types, which means when we create an instance of a struct, that particular instance keeps a copy of its own data. On the other hand, classes are reference types, which means when you create an instance of a class, all instances share a copy of the data.
You’ll also learn about CustomStringConvertable, which is a protocol you’ll use to return a descriptive string value from a struct even though the struct looks like it won’t return a string.
You’ll learn how to create and use a Stack, a very traditional data structure used by many programming languages, and you’ll learn how to create and use Generics, which will help you break away from using a highly specified type for more flexible code.
Learning Objectives
- Describe structs
- Explain the difference between value and reference types
- Identify Stacks and Generics
Skills you’ll gain
Data StructuresData-Structured LanguageSemi-Structured DataStructured ProgrammingStructured TextSwift (Programming Language)What You'll Learn
- Review Swift struct syntax and organization
- Explain the difference between value types and reference types
- Use the CustomStringConvertible protocol to return a descriptive string value from a struct
- Create and use a Stack data structure
- Create and use Generics for more flexible, less type-specific code
Key Takeaways
- Swift offers several ways to structure data, including structs, classes, stacks, and generics.
- Structs are value types, so each instance of a struct keeps a copy of its own data.
- Classes are reference types, so all instances share a copy of the data.
- CustomStringConvertible is a protocol used to return a descriptive string value from a struct.
- Generics help you move away from a highly specified type so your code can be more flexible.
Frequently Asked Questions
What does this course cover?
This module covers Swift structs (including struct syntax and organization), the difference between value types and reference types, the CustomStringConvertible protocol, and how to create and use Stacks and Generics.
What is the difference between value types and reference types in this course?
Structs are value types, meaning each instance keeps a copy of its own data, while classes are reference types, meaning all instances share a copy of the data.
What skills will I gain from this course?
You will gain skills in Data Structures, Data-Structured Language, Semi-Structured Data, Structured Programming, Structured Text, and Swift (Programming Language).
What lessons are included?
The lessons are Review of Structs, Value Types, CustomStringConvertible, Using Stacks, and Using Generics.
What is CustomStringConvertible used for?
CustomStringConvertible is a protocol you use to return a descriptive string value from a struct, even though the struct looks like it won't return a string.
Transcript
Show transcript (free preview lesson)
Transcript of the free preview lesson. Remaining lessons unlock with the full course.
Hello, my name is Mark Nair, and in these lessons, you'll learn about data structures in Swift. We'll review structs, and then we'll look at value types. How do we use custom string convertible, stacks, and generics? Let's quickly review Swift structures or strucs. We use strucs when we need to create an entirely new type of data structure to work with. Let's take a look. Here, I'm going to declare a bicycle structure with the properties of name, tire, PSI, type, weight, and whether the bicycle is a comfy ride. So we start the keyword struct, and then we name our struct. In this case, I'll name it bicycle. And then we end this with curly braces, and inside of the curly braces we will put the properties of the strut. Now, since this is a property with no value, I'm gonna have to give it a type. So that's a string. So you can see here that I'm using different types name and brander strings. The PSI of the tire is an int, the weight of the bicycle is a double, and whether it's a comfy ride is a Bool. So now that I've made my structure, this means it is its own type. Now I can use this by creating an instance of this struct. Think about it like this. The struct is a blueprint, and that instance is the actual building we create from the blueprint. It looks like this. It's gonna make a new variable called touring bike. And that's gonna be equal to the type, which is gonna be the struct bicycle. Now, when I type the open parentheses in the popup I will see the values of the properties. I can just hit return, accept this, and now I can easily just tab from one to the other and put in the values. So what I've done here is I've said the name of the bike is Freyja, the brand is a Riese and Müller, the tire PSI is 25, the weight, pretty heavy, 64 pounds, and is it a comfy ride, true, it is. Let's make another instance. This is for road bike. Again, the bicycle is the type, this struct or the type. Open parenthesis gives me all my stuff, and then I can fill in the rest. Just like that. Now, to access this information, let's just attribute it to a new constant. And inside of here, I'll just put a string. Now, what I'd like to say is whatever the type of bike, the name of the bike, has a specific weight. And the way I do this is I access this information of the struct using dot syntax. So touring bike dot, and then there is my other information of the properties. I'll hit name. And then over here, touring bike dot weight. There we go. My Freyja bicycle weight, 64.3 pounds. Now really, maybe I'll use brand here, let's try this again. My Riese and Müller bicycle weighs 64.3 pounds. Pretty good. Let's try another one. So here's a structure named animal. Now we'll make an instance of animal. Same thing, here's my struct and then open parenthesis. Here are my properties, and then the species. Tiger has four legs and a tiger, yes, is an omnivore Now, to access that information, I'll just call Tony the Tiger and omnivore, and the result will be true. There it is. Now, before we can use an instance of our struct we have to initialize the instance. And here I'm initializing it inside of here. And I'll explain this in a second, but if I do something like this, get rid of this line. That doesn't work. So I'm trying to make Tony the Tiger destruct, but I can't because I don't have any values. I'm missing arguments in the parameters. So we could give the structure some default values like this. Now, when I give these values here, of course Swift is smart enough to realize what the type is, so I do not have to be explicit about that any longer. And just to remove any extra complexity in the code. But now if I look at Tony The tiger, and I call the dot species on it, I see that I have ape, but Tony the Tiger is a tiger, not an ape. So the default values, I want to override those default values. So that's when we use the memberwise initializer that we looked at a second ago. So up here, Tony, the tiger, it's the same thing except this parenthesis. I'm not gonna accept these default values. I'm going to override them by selecting this. You'll see here in the pop-up window, I have parenthesis and then parenthesis with the properties inside of it. The second one, just the parenthesis, is when I'm just going to accept the default values. But the first one is I'm gonna tell Swift, I need to do a little more work on this. Now, Tony, the Tiger is a tiger, and if I look at number of legs, four, great. But if I look at the status, it is true, even though I did not say in the memberwise initializer that Tony the Tiger is true to be an omnivore, I'm pulling that from the default value here of the struct so I don't have to be specific. All right, that is a quick overview of structs. Thanks for watching. Stay tuned for the next lesson where I'll show you what a value type is.
Learn on the Go
Take your learning anywhere — the KnowledgeCity mobile app lets you watch lessons on the go.