Image for post
Image for post

An enumeration is a data type consisting of a set of named values, called members. Apple doc says:

An enumeration defines a common type for a group of related values and enables you to work with those values in a type-safe way within your code.

Enumerations in Swift are much more flexible, and do not have to provide a value for each case of the enumeration. If a value (known as a “raw” value) is provided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.

Enumeration Syntax

You introduce enumerations with the enum keyword and place their entire definition within a pair of braces:

example with type:

example without type:

The values defined in an enumeration (such as Male and Female) are its enumeration cases. You use the case keyword to introduce new enumeration cases.

Alternatively, you can declare it like :

Each enumeration definition defines a brand new type. So, obviously it should have a name starting with capital letter.

We can now set a variable to a enumeration case like so:

var gender: Gender = Gender.male or in short as

var gender = Gender.male

Switch Statement using enums :

Note: In the above example, we don’t use the default case as all the possible enum members are taken care. Only what’s in our enums needs to be checked for. If you do not exhaust all the enum members, then you should add the default case.

User defined values :

Enum case cannot have a raw value if the enum does not have a raw type.

If enum type is defined, then it cannot have other datatypes as enum member cases.

You could use the enum with defined values if enum is having a raw type:

This now enumerates the values and assigns genre.One with 1001 and genres.Two with 1002 and so on.

Note that with this declaration we have also added the var type after the name of the enum. To get the value stored in that enum member, you need to access it’s rawValue property as
println("Result -> \(genres.Two.rawValue)") // prints "1002"

When integers are used for raw values, they auto-increment from previous value if no value is specified.

Now, consider the above enum of genres. If one equals 1001, two is given a value of 2000, and if three is not given any value, then the raw value of three will give you 2001.

Enums with methods:

Enums can have methods which can be used on enum cases:

Enum with Associated Values

The following code is copied from

Important: Enums can have methods, subscripts, and computed properties. But it cannot have stored properties.

That’s it. !!


If you enjoyed reading this post, please share and give some clapps so others can find it 👏👏👏👏👏 !!!!

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn.

If you have any comment, question, or recommendation, feel free to post them in the comment section below!

Written by

iOS and tvOS developer, dreamer, photographer 🤨

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store