01. Nested Enumeration

It will take about 1 minutes to finish reading this article.

Example Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum Character {
enum Weapon {
case Bow
case Sword
case Lance
case Dagger
}
enum Helmet {
case Wooden
case Iron
case Diamond
}
case Thief
case Warrior
case Knight
}
//Access like this:
let character = Character.Thief
let weapon = Character.Weapon.Bow
let helmet = Character.Helmet.Iron

When accessing nested enumeration members, it is not necessary to enter such a long level every time to access them. You can use convenient methods to access them directly.

1
2
3
4
5
6
7
8
9
func strength(of character: Character, 

with weapon: Character.Weapon,

and armor: Character.Helmet) {

}
strength(of: .thief, with: .bow, and: .wooden)