Site icon Ravi Shankar

Enum in Swift

Enum group of related values to a single data type. Swift enum has lot of new features compared to its predecessor Objective-C. Let us see this with an example enum type for all Months in a year.

enum Months {

case January, February, March, April, May, June, July, August, September, October, November, December

}

 

enum Month {

case January, February, March, April, May, June, July, August, September, October, November, December

}

Now you can define a variable or a constant of type month as shown below.

let currentMonth = Month.May

And variable or constant is declared with the data type then you can use the short form.

let currentMonth:Month = .May

 

Enum with Raw Value

Enum in Swift can be assigned a RawValue when declaring and the value can be of any data type. Let us day you want to specify the String value of each Month

enum Month: String {

case January = “January”, February = “February”, March = “March”, April = “April”, May = “May”, June = “June”, July = “July”, August = “August”, September = “September”, October = “October”, November = “November”, December = “December”

}

The RawValue can be printed by accessing .rawValue on the enum variable or constant.

let currentMonth:Month = .May

currentMonth.rawValue

Enum with Associated Value

 

Same like RawValue, enum can also have associated value and of data type.

enum Month {

case January(String), February(String), March(String), April(String), May(String), June(String), July(String), August(String), September(String), October(String), November(String), December(String)

}

let currentMonth:Month = .May(“Summer Vacation”)

switch currentMonth {

case .May(let message):

println(message)

default:

println(“No Values”)

}

In the above example, Month enum values are declared with an associated value of data type String. And while assign the enum value the associated value is also provided to the currentMonth constant. Using the switch the associated value is retrieved.

Enum can have member function

 

Enum in Swift can also have member function. In the below example code, we have declared the Month enum with rawValue of type Int. Now add a member function to calculate the number of months left from the currently assigned value.

enum Month: Int {

case January = 1, February, March, April, May, June, July, August, September, October, November, December

func monthsLeftForYearEnd() -> Int {

   return Month.December.rawValue self.rawValue

}

}

let month: Month = .May

month.monthsLeftForYearEnd()

Constant month is assigned a enum value of .May and you can find out the number of months left for the year end by accessing the monthsLeftForYearEnd.

Enum and Initialiser

 

Swift’s enum can have initialiser too where you can set the default enum value or do some initial processing. If you add init function as shown below, the default enum value will be set to July.

enum Month: Int {

case January = 1, February, March, April, May, June, July, August, September, October, November, December

init() {

self = .July

}

func monthsLeftForYearEnd() -> Int {

return Month.December.rawValue self.rawValue

}

}

let month = Month()

month.monthsLeftForYearEnd()

 

Exit mobile version