Member-only story
Enums in swift
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…