What is Enum?


Object-Oriented Programming
What is OOP?
9m 33s

The course is part of this learning path

Start course
1h 22m

This course delves into Object-Oriented Programming (OOP), covering its main components, and then putting these into practice by walking you through the creation of an app using OOP.

Learning Objectives

  • Learn about classes and how you can leverage them in your code.
  • Learn concepts such as Enum, Inheritance, and Access Levels
  • Understand how these can be used to build apps

Intended Audience

This course is intended for beginners who want to learn how to build apps using Swift.


To the most out of this course, you should have some basic understanding of programming and computer science in general.



Hi. Within this lecture, we're going to see how we can leverage this init function in order to create these properties without initializing them. So, I'm deleting these initializers and I'm going to do this inside of my init function, okay?

So, rather than printing Musician created, I'm just going to ask for some input in the init function. So, I'm going to ask for nameInit, it will be a string, okay? And again, an ageInit. This will be an integer. And an instrumentInit. This will be again a string. So, what I'm going to do, I'm going to take those values and I'm going to say name = nameInit. Age = ageInit, okay? So, you're going to have to say name = nameInit. Age = ageInit. And of course, the instrument = instrumentInit. Yo0 don't have to say init, you can just go for name, you can just go for whatever variable name you want. I'm just trying to differentiate them a little bit so you don't get confused, okay? So, whenever I create an instance of this class, it will ask me for a name and age and instruments. So, rather than defining those values after we create the object, now we can do this in the same line. So, let me do an example. So, if I say let James, and James is a musician, it should have asked me there. So, let me do command B. This is asking right now.

So, let me do this from the beginning, okay? So, if I do command B, it will get synchronized as I said before now it's giving me an error. It says that you have a missing argument in here. Now, if you open parenthesis, you will see that it asks you for a nameInit, ageInit, and instrumentInit. So, for name you can go for James, okay? For age you can go for 50, and for instrument you can go for guitar. So, now if I print james.age it will give me 50. So, that's how you create objects with initializers, okay? And while we are at it, I believe we should learn about Enums as well.

So, Enums make our life easier when we try to categorize things. What do I mean? For example, if I want to add a type of musician in here as a property, I can just go and do that with a string, right? But rather than doing a string, I'm just going to go for Enum and you will see why I'm doing that in a clear way. So, generally, you see Enums, Enumerated Type Declarations in the top of this project files, okay? So, you have to give a name here, an Enum name like MusicianType. And in here you write some categories, for example our musician can be a vocalist, guitarist, or lead guitarist, right? So, write them all, so LeadGuitar, it would be a Vocalist, it will be a Drummer, right? And it would be a Bassist as well. So, maybe you can think something like a Violinist or something, okay?

So, why are we creating these types? We can have done this with strings as well. I'm going to show you why. If I come over here to my attributes, I can easily add a new type. I can just say type, okay? And this would be a string. But if I do that, my other developer working on this project can write vocal, okay? And the other developer using this class can write vocalist. And even if you are the only one working on a project like this, you can do the same mistake as well, right? You can go for vocal, you can go for vocalist in some other cases, maybe you want to standardize those types.

So, this is where Enums come to play. You kind of standardize the categories. So, that wouldn't leave no room for mistake at all. So, rather than saying this is going to be a string, I'm going to say this will be a MusicianType, okay? As you can see, MusicianType is a MusicianType type. So, this is an Enum, this is not a class, but we can actually use this to choose a category. And now we have an error inside of init because we have to ask for a typeInit, right? So, I'm going to go for typeInit, and this will be a MusicianType rather than string.

Now, I can come over here and I can easily say my type, this type, okay? Is going to be a typeInit. So, right now, we have created a category for ourselves and we have assigned it to be a property in our class.

So, let me do a build so it will get synchronized and once it gets synchronized as you can see, we got another error in here. So, it says that you have to add a new parameter here called typeInit. If you hit 'Fix', this will be added automatically so I can hit that here and I can choose one of the categories that we have created. So, I can easily come here, I can say James is a vocalist for example. And now I can reach this value as well, right? So, let me try to print this james.type. Now I will see the type in the logs as well. So, this is how you use Enums. This is how you leverage Enums in your classes. If you're looking for a way to create categories rather than doing this with a string, you can do this with Enums and this would be much more efficient, much more safer than doing this with a string. So, that's all for right now. We're going to stop here actually. And within the next lecture, we're going to see how we can add some actions, some functions to our class as well.

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