An enumeration is a data type consisting of a set of named values, called members. Apple doc says:
original source : https://medium.com/@abhimuralidharan/enums-in-swift-9d792b728835
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) isprovided for each enumeration case, the value can be a string, a character, or a value of any integer or floating-point type.
You introduce enumerations with the enum keyword and place their entire definition within a pair of braces:
// enumeration definition goes here
enum Gender :String{
case Male
case Female
}
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
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.
If you had the enum as we had declared earlier without the type, there is no rawValue available but instead you get a member called hashValue. All enums get a hashValue which is basically like an Index to the order in which the enums were declared. So in the enum called Gender declared above, malewill have a hashValue of 0 and female will have a hashvalue of 1. The reason for this is that hashValues are based on the first item having the index value of 0.
The rawValue on the other hand is a type value that you can assign to the enum members.
enum Gender :String { // enum with type
print(Gender.male) // prints “male”
print(Gender.male.rawValue) // prints “I am male”
print(Gender.male.hashValue) // prints 0
accessing values with enums without type:
enum Gender { // enum without type
print(Gender.male) // prints “male”
print(Gender.male.rawValue) // Error. rawvalue cannot be accessed for enums without type
print(Gender.male.hashValue) // prints 0
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.
case male = 1 //error: enum case cannot have a raw value if the enum does not have a raw type
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 can have methods which can be used on enum cases:
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")
}
If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!