https://appventure.me/2015/10/17/advanced-practical-enum-examples/
http://brettbukowski.github.io/SwiftExamples/examples/enums/
Basics
When creating an enum for use, make sure you declare public. Whatever name labels
you declare do not implicitly map to values. Rather, they are their own values.
When a variable is assigned to an enum’s member, and gets printed, it will literally
print the the enum member.
1 2 3 4 5 6 7 8 9 10 |
public enum Example { // the named labels do not implicitly map to values such as 0, 1.. etc. case A // enum members are their own values case B // when printed, it will literally print this value i.e B case C case D case None case Str(String) // you declare other types and their respective values } |
Say we assign a variable to enum’s C.
1 2 |
var example = Example.A example = .C // |
..and you print out the enum:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
switch example { case .A: print("\(example) Hadooo ken!") case .B: print("\(example) Sho Ryu Ken!") // B case .C: print("\(example) Tats Maksen Bukaku!") case .D: print("\(example) You must Defeat....") case .None: print("\(example) ...DO NOTHING...") default: print("\(example) default") } |
output:
C Tats Maksen Bukaku!
It prints C because we assigned our var to Example.C
Assigning other types to your enum values
1 2 3 4 5 6 7 |
public enum Example { // the named labels do not implicitly map to values such as 0, 1.. etc. case A // enum members are their own values case None case Str(String) // you declare other enum types along with other types } |
When you print it, you will literally get the enum value, and its type.
1 2 3 |
var a = Example.A a = .Str("hehe") print(a) |
output:
Str(“hehe”)
Also, nil cannot be assigned (or initialized) to Example.
1 2 |
var a : Example = nil print(a) // error |
Raw Values
Raw values are compile-time set values directly assigned to every case within an enumeration, as in the example detailed below:
1 2 3 4 5 |
enum Alphabet: Int { case A = 1 case B case C } |
In the above example code, case “A” was explicitly assigned a raw value integer of 1, while cases “B” and “C” were implicitly assigned raw value integers of 2 and 3, respectively.
To Raw Values
given…
1 2 3 4 5 6 7 |
// we declare enumeration 'Example2' of raw-value type String public enum Example2 : String { case GuileMove = "Sonic Boom" case DhalsimMove = "Yoga Fire" case SagatMove = "Tyyguuur!" case None } |
We can declare enum of certain type. This means that the enum values can contain Strings, Ints…etc.
Here, we have a enum that contains Strings. Even though we can simply use the enum values, those enum values also have raw values, which are the strings that we assign them to.
For example, here, have a whole bunch of enum values, they are enough and can be used. However, they also have String values attached to them.
In order to get the string values, you can use rawValue of the enum value.
1 2 3 4 5 6 7 8 9 |
// num with type String public enum Example2 : String { case GuileMove = "Sonic Boom" case DhalsimMove = "Yoga Fire" case SagatMove = "Tyyygur!" } var Guile = Example2.GuileMove // get the enum value GuileMove into the variable print(Guile.rawValue) // then access the raw value, outputs 'Sonic Boom' |
From Raw Values
You can also declare the enum with the raw value, and try to get the enum value back.
The enum value returned is an Optional type.
skill contains the enum value name ‘GuileMove’, which is an Optional Type
skill has 2 properties: hashValue, and rawValue.
1 2 3 4 |
var skill = Example2(rawValue: "Sonic Boom")! print(skill.hashValue) // 0 print(skill.rawValue) // Sonice Boom print(skill) // GuileMove |
Associated Values
Associated values allow you to store values of other types alongside case values, as demonstrated below:
1 2 3 4 5 |
enum Alphabet: Int { case A(Int) case B case C(String) } |
Other Examples
1 2 3 4 5 6 |
enum Movement: Int { case Left = 0 case Right = 1 case Top = 2 case Bottom = 3 } |
1 2 3 4 5 6 |
enum Constants: Double { case π = 3.14159 case e = 2.71828 case φ = 1.61803398874 case λ = 1.30357 } |
1 2 3 |
enum Planet: Int { case Mercury = 1, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune } |
1 2 3 4 5 6 7 8 9 |
// You can also map to strings enum House: String { case Baratheon = "Ours is the Fury" case Greyjoy = "We Do Not Sow" case Martell = "Unbowed, Unbent, Unbroken" case Stark = "Winter is Coming" case Tully = "Family, Duty, Honor" case Tyrell = "Growing Strong" } |
Enum conform to Protocol
1 2 3 |
protocol CustomStringConvertible { var description: String { get } } |
implement function description.
1 2 3 4 5 6 7 8 9 |
enum Trade: CustomStringConvertible { case Buy, Sell var description: String { switch self { case Buy: return "We're buying something" case Sell: return "We're selling something" } } } |
Other examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
enum Months : Int { case Jan = 1, Feb, Mar, Apr, June } print("Months.Jan - \(Months.Jan)") print("Months.Jan.rawValue - \(Months.Jan.rawValue)") enum Trade { case Buy, Sell, Xmas, Thanksgiving // case these enumerations, let it run through a function var anotherFunc: Int { if (self == .Buy) { return 88 } else if (self == .Sell) { return 66 } else if (self == .Xmas) { return 1226 } else if (self == .Thanksgiving) { return 1125 } return -1 } var description: String { switch self { case .Buy: return "We're BUYING something" case .Sell: return "We're SELLING something" case .Xmas: return "Christmas gifts!" case .Thanksgiving : return "Turkey, cranberry sauce, and rice!" } } //description } print("Magic Number - \(Trade.Xmas.anotherFunc)") // 1226 print("Trade.Buy - \(Trade.Buy)") print("Trade.Buy.description - \(Trade.Buy.description)") print("Trade.Buy - \(Trade.Sell)") print("Trade.Buy.description - \(Trade.Sell.description)") |
Multiple cases, one code block
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
var someOptional : Int? = 6680 switch someOptional { case 123? : print("its 123") case 6680? : print("its 6680") default: print("ITS NIL :(") } var optionalString : String? = "703" switch optionalString { case "703"?, "702"? : print("----------------") for i in 0 ..< 8 { print(i) } default: print("-----------") } |