Nested Enums

Welcome to Lesson 3 of Advanced Enum. You will learn how to add multiple enums within an enum to design organized structure and design. This lesson will take your game to the next level.

Problem

Just more than one

Intro to Nested Enums

Create an enum called FutureCourse. It contains other enums called, LearnSwiftWithBob and UIKitFundamentals.

  1. enum FutureCourse {
  2. enum LearnSwiftWithBob: String {
  3. case genericProtocol
  4. case advancedEnum
  5. var chapaterDescription: String {
  6. return self.rawValue
  7. }
  8. }
  9. enum UIKitFundamentals: String {
  10. case realmDatabase
  11. case noStoryboard
  12. case cloudComputing
  13. var chapterDescription: String {
  14. return self.rawValue
  15. }
  16. }
  17. }
  18. Initialize an object
  19. ```swift
  20. FutureCourse.LearnSwiftWithBob.advancedEnum.chapaterDescription
  21. FutureCourse.UIKitFundamentals.cloudComputing.chapterDescription

Game Design

Create a struct called, Character. Add a nested enum called CharacterType and Skill. The Character struct has two property whose types are CharacterType and Skill.

  1. struct Character {
  2. enum CharacterType {
  3. case prince
  4. case warrior
  5. case priest
  6. }
  7. enum Skill {
  8. case airwalk
  9. case transparency
  10. case prediction
  11. }
  12. let type: CharacterType
  13. let skill: Skill
  14. }

To initialize, you all have to pick and choose.

  1. let warrior = Character(type: .priest, skill: .airwalk)

Important: Follow this pattern for designing your app.

Another Game Design

Instead of using a struct as above, you may achieve the similar outcome using enums only.

  1. enum Wearable {
  2. enum Weight: Int {
  3. case light
  4. case medium
  5. case heavy
  6. }
  7. enum Price: Int {
  8. case leather
  9. case iron
  10. case steel
  11. }
  12. case armor(weight: Weight, price: Price)
  13. // case houseMaterial(weight: Weight, price: Price)
  14. func getDescription() -> String {
  15. switch self {
  16. case let .armor(weight, price) :
  17. return "You've chosen \(weight) and \(price)"
  18. }
  19. }
  20. }
  1. let myClothes = Wearable.armor(weight: .heavy, price: .steel)
  2. myClothes.getDescription()
  3. // "You've chosen heavy and steel"

You don’t have to create structs/classes for every design.

Source Code

7003_nested_enum.playground

Conclusion

You’ve learned how nested enums may be useful for replacing classic structs and classes. I recommend you use both of them. When you design APIs for your teammates or open source libraries, attempt to use enums for anything has to do with choosing options because the user of your code doesn’t’ have to type or go through multiple properties and methods all mixed together. Instead, enums allows him/her to pick and choose available cases for the particular enum. The skill that requires you to code in a different league is deploying enums in the right context.

In the following lesson, you will learn how to combine enums with protocols.

Note: Learn Swift with Bob is available on Udemy. If you wish to receive a discount link, you may sign up here.