-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathenums.swift
83 lines (65 loc) · 1.7 KB
/
enums.swift
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
enum Example {
case A
case B
case C
case D
}
// Unlike enums in other languages, the named labels
// do not implicitly map to 0, 1.. etc. enum members
// are their own values of the type specified by the
// enum's name.
var example = Example.A // (Enum Value)
// Once you assign to an enum value, you can reassign
// to another value without respecifying the the enum
// name.
example = .B
// Switch statements must be exhaustive or declare
// a default case.
switch example {
case .A:
print("A")
case .B:
print("B") // B
case .C:
print("C")
case .D:
print("D")
}
// Enumerations can store values of any type, and
// type values can be different for every enum member.
enum Types {
case Str(String)
case Num(Double)
}
// A variable can be reassigned a different type of the
// enum.
var a = Types.Str("hello")
a = .Num(1.0)
// Associated values can be extracted as part of a switch.
switch a {
case .Str(let val):
print(val)
case .Num(let val):
print(val) // 1.0
}
// # Raw Values
// Enums can prepopulate with "raw" values, similar to other
// languages.
enum Letters: Character {
case a = "A"
case b = "B"
case c = "C"
}
// When integers are used for raw values, they
// auto-increment if no value is specified.
enum Numbers: Int {
case One = 1, Two, Three, Four, Five
}
// Access raw values with `toRaw`
var five = Numbers.Five
print(five.rawValue) // 5
// `fromRaw` tries to find an enum member with a raw value.
// An optional is returned.
var possibleNum = Numbers(rawValue: 2)!
print(possibleNum == Numbers.Two) // true
// TK - type methods and mutating methods