Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the count of a Swift enum?

Tags:

enums

count

swift

People also ask

How do you count enums?

You can use Enum. GetNames to return an IEnumerable of values in your enum and then. Count the resulting IEnumerable.

Can enums have numbers?

Numeric enums are number-based enums i.e. they store string values as numbers. Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.

How does enum work in Swift?

What is a Swift Enum? According to the Swift documentation enumeration is defined as “a common type for a group of related values and enables you to work with those values in a type-safe way within your code”. Think of it as a type of variable that is specifically used switch/conditionals.

What is enumerate in Swift?

In Swift, an enum (short for enumeration) is a user-defined data type that has a fixed set of related values. We use the enum keyword to create an enum. For example, enum Season { case spring, summer, autumn, winter } Here, Season - name of the 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

I have a blog post that goes into more detail on this, but as long as your enum's raw type is an integer, you can add a count this way:

enum Reindeer: Int {
    case Dasher, Dancer, Prancer, Vixen, Comet, Cupid, Donner, Blitzen
    case Rudolph

    static let count: Int = {
        var max: Int = 0
        while let _ = Reindeer(rawValue: max) { max += 1 }
        return max
    }()
}

Xcode 10 update

Adopt the CaseIterable protocol in the enum, it provides a static allCases property which contains all enum cases as a Collection . Just use of its count property to know how many cases the enum has.

See Martin's answer for an example (and upvote his answers rather than mine)


Warning: the method below doesn't seem to work anymore.

I'm not aware of any generic method to count the number of enum cases. I've noticed however that the hashValue property of the enum cases is incremental, starting from zero, and with the order determined by the order in which the cases are declared. So, the hash of the last enum plus one corresponds to the number of cases.

For example with this enum:

enum Test {
    case ONE
    case TWO
    case THREE
    case FOUR

    static var count: Int { return Test.FOUR.hashValue + 1}
}

count returns 4.

I cannot say if that's a rule or if it will ever change in the future, so use at your own risk :)


I define a reusable protocol which automatically performs the case count based on the approach posted by Nate Cook.

protocol CaseCountable {
    static var caseCount: Int { get }
}

extension CaseCountable where Self: RawRepresentable, Self.RawValue == Int {
    internal static var caseCount: Int {
        var count = 0
        while let _ = Self(rawValue: count) {
            count += 1
        }
        return count
    }
}

Then I can reuse this protocol for example as follows:

enum Planet : Int, CaseCountable {
    case Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune
}
//..
print(Planet.caseCount)

Create static allValues array as shown in this answer

enum ProductCategory : String {
     case Washers = "washers", Dryers = "dryers", Toasters = "toasters"

     static let allValues = [Washers, Dryers, Toasters]
}

...

let count = ProductCategory.allValues.count

This is also helpful when you want to enumerate the values, and works for all Enum types


If the implementation doesn't have anything against using integer enums, you could add an extra member value called Count to represent the number of members in the enum - see example below:

enum TableViewSections : Int {
  case Watchlist
  case AddButton
  case Count
}

Now you can get the number of members in the enum by calling, TableViewSections.Count.rawValue which will return 2 for the example above.

When you're handling the enum in a switch statement, make sure to throw an assertion failure when encountering the Count member where you don't expect it:

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  let currentSection: TableViewSections = TableViewSections.init(rawValue:section)!
  switch(currentSection) {
  case .Watchlist:
    return watchlist.count
  case .AddButton:
    return 1
  case .Count:
    assert(false, "Invalid table view section!")
  }
}

This kind of function is able to return the count of your enum.

Swift 2:

func enumCount<T: Hashable>(_: T.Type) -> Int {
    var i = 1
    while (withUnsafePointer(&i) { UnsafePointer<T>($0).memory }).hashValue != 0 {
        i += 1
    }
    return i
}

Swift 3:

func enumCount<T: Hashable>(_: T.Type) -> Int {
   var i = 1
   while (withUnsafePointer(to: &i, {
      return $0.withMemoryRebound(to: T.self, capacity: 1, { return $0.pointee })
   }).hashValue != 0) {
      i += 1
   }
      return i
   }