How to get all swift enum cases as an array

If you require to have all the cases of a specific enumeration as an array then you can use CaseIterable. You need to write CaseIterable after enumeration name and swift will expose a property allCases which is collection of all enumeration cases

enum Beverage: CaseIterable {
    case coffee, tea, juice
}
let numberOfChoices = Beverage.allCases.count
print("\(numberOfChoices) beverages available")
for beverage in Beverage.allCases {
    print(beverage)
}

NEED A JOB? NEED TO HIRE? NEED HELP?

Join group of Software Professionals

iOS DEVELOPER’S DEN