How do I get the count of a Swift enum?

As of Swift 4.2 (Xcode 10) you can declare
conformance to the CaseIterable protocol, this works for all
enumerations without associated values:

enum Stuff: CaseIterable {
    case first
    case second
    case third
    case forth
}

The number of cases is now simply obtained with

print(Stuff.allCases.count) // 4

For more information, see

  • SE-0194 Derived Collection of Enum Cases

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)