Enums in swift

Abhimuralidharan
3 min readMay 17, 2017

--

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:

enum SomeEnumeration {// enumeration definition goes here}

example with type:

enum Gender :String{
case male
case female
}

example without type:

enum Gender {
case male
case female
}

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 :

enum Gender :String {
case male, female // in a single line like this, separated by commas
}

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 :

switch gender {
case .male: print(“Gender is male”)
case .female: print(“Gender is female”) // default case is not needed
}

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.

enum Gender {case male = 1 //error: enum case cannot have a raw value if the enum does not have a raw typecase female}

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

enum Genres:Int{
case One = 1001, Two, Three, Four, Five
}

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.

Enum Genres:Int{
case One = 1001, Two = 2000, Three, Four, Five
}
print(Genres.Three) // prints "2001"

Enums with methods:

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

enum WeekDay :String {case Monday
case Tuesday
func day() ->String { return self.rawValue }}print(WeekDay.Monday.day()) // prints Monday

Enum with Associated Values

The following code is copied from Tutorialspoint.com.

enum Student {
case Name(String)
case Mark(Int,Int,Int)
}
var studDetails = Student.Name("Swift")
var studMarks = Student.Mark(98,97,95)
switch studMarks {
case .Name(let studName):
println("Student name is: \(studName).")
case .Mark(let Mark1, let Mark2, let Mark3):
println("Student Marks are: \(Mark1),\(Mark2),\(Mark3).")
default:
println("Nothing")
}

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

That’s it. !!

Enjoy!!

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!

--

--