Image for post
Image for post

Enumeration Syntax

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

Switch Statement using enums :

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

User defined values :

enum Gender {case male = 1 //error: enum case cannot have a raw value if the enum does not have a raw typecase female}
enum Genres:Int{
case One = 1001, Two, Three, Four, Five
}
Enum Genres:Int{
case One = 1001, Two = 2000, Three, Four, Five
}
print(Genres.Three) // prints "2001"

Enums with methods:

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

Enum with Associated Values

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")
}

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

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