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

Creating Struct


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

The course is part of this learning path

Start course

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 create our first struct, so that we would understand everything I told you in the previous lecture in a much better way. So, rather than selecting the SingleView app, I'm going to go for macOS and select the Command Line Tool. Remember, this provides us the opportunity to run our code inside the command line rather than a simulator. So, we're going to be fast like we did in the IntermediateSwiftSection. Okay. So, I'm going to call this something like AdvancedSwiftProject, okay? And we're going to see a lot of concepts inside of this project. We're not going to see only structs, but we're going to see other concepts like tuples, guard let as well. So, let me make this into full screen. And if you have skipped the intermediate section for some reason, if you just came here directly, as you can see if I hit 'run' on here, it will just compile this code on my Mac without running a simulator and we will see the results in log immediately. Of course, this is the first build, so it's taking its time, but it will be much more faster along the way. So, rather than printing Hello World, I'm going to create a struct and I'm going to print the structs. So, how do we do that? I'm going to follow the same example like we did in the IntermediateSwiftSection. So, I'm going to come over here to create a new file. This will be a Swift File, and in my first Swift File, I'm going to have a class of musicians, okay? I'm going to call this MusiciansStruct rather than class and don't worry, I'm going to have a MusicianClass as well. I'm going to create another files with file, Swift file. I'm going to call this MusicianClass. So, I will create one struct and one class and they will have all same attributes. So, let's start with class because we already know how to do that. So, in order to create a class, we use keyword class and I will say MusicianClass. So, under my MusicianClass, I will have three properties: I will have a name, which is a string; I will have an age, which is an integer; and I will have an instrument, which again is a string. So, if I do that, I will get an error from this class because we don't have the initializing function yet. Remember, we have to have a constructor here in order to start and create an object out of this class. So, if I say init, it will be okay for us and inside of this init, I'm going to take a name input and this will be a string. I'm going to take an age input, this will be an integer and I'm going to take an instrument input as well, this will be again as string. So, under this init, I'm going to say self.name is name input, okay? So, self.age = ageInput and self.instrument = instrumentInput. So far so good. Now we can create an object out of this class and this will be the first function to be executed when we do that. So, we assign the attributes with related values. So, let's do that with musicians struct, right? So, let's come over here first. Let's try to create an object out of this class. So, let me say, let classJames for example, and this will be a MusicianClassObject. And when I do that, it has to auto complete, but it doesn't work right now, so I'm going to delete this and hit command beyond my computer to build this and then I can call MusicianClass. If I open parentheses, it will show me the parameters and for name input, I'm going to go for James, for age: 50, and for instrument: guitar. And let me try to print out the classJames.age for example. So, if I run this, I will see 50 in my logs as expected. Now let's do that with struct. Let's go to struct and rather than class, I'm going to start by saying struct, so we use the actual keywords struct in this case as well. So, struct MusiciansStruct, okay? And let me open this parentheses, we're going to have the same attributes here and name and age and then instruments. So, our instruments will be string. So, maybe you realize the difference right now, even though I don't have any initializer, I don't get any error. So, if I do 'Cmd + B', I still don't get any error. So, this is a great feature of struct. If I come over here to main, I can say let structJames and I will create an instance of this MusiciansStruct, okay? If I open parentheses, it will automatically show me the parameters. So, this is called a Free Initializer, and we get this initializer even without writing the initializer itself, this is a great feature, so I can just come over here and give the exact same attributes like James 50 and guitar, and I can come over here and say structJames.age and it will give me the same results. So, let me run this and here you go. We have two 50s over here. So, this is good, we know how to create struct and we know how to create class right now. And in order to understand the technical differences between them, we have to dig a little bit deeper. For example, we have to understand the true immutability and we have to understand the behavior difference between the reference type and value type. So, remember struct is a value type, whereas class is a reference type, so they have some real major differences in terms of technicality and we're going to talk about all of these things in the following lectures; don't worry. Right now, you have seen how easy it is to work with structs and how easy it is to create one. So, we're going to stop here. And following the next lecture, we're going to dig a little bit deeper to understand it more comprehensively.

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