I'm trying to figure out a way to retrieve all elements in a nested enum from an array. The way I built my nested enum as follows:
enum ItemType: Hashable {
case sword(Swords)
case shield(Shields)
case armor(Armors)
}
enum Swords: String, CaseIterable {
case Sabre = "Sabre"
case Xiphos = "Xiphos"
case Broadsword = "Broadsword"
}
enum Shields: String, CaseIterable {
case Buckler = "Buckler"
case HeaterShield = "Heater Shield"
case KiteShiled = "Kite Shield"
}
enum Armors: String, CaseIterable {
case StudArmor = "Stud Armor"
case Chainmail = "Chainmail"
case PlateArmor = "Plate Armor"
}
Now I have an array that contains some of the elements from the ItemType enum:
let loots: [ItemType] = [
.sword(.Xiphos),
.sword(.Broadsword),
.sword(.Sabre),
.armor(.StudArmor),
.armor(.Chainmail),
.shield(.Buckler),
.shield(.HeaterShield)
]
Is there a way to get all elements under ItemType.sword into another array? I'm thinking along the line of: (obviously it doesn't work)
let item = loots.first(where: {$0 == ItemType.sword})
How can I achieve my goal? Or do I have to reconstruct my Enums in a different way?
Please let me know.
Thank you in advance.