How to enumerate an enum with String type?

Lucien picture Lucien · Jun 3, 2014 · Viewed 244.6k times · Source
enum Suit: String {
    case spades = "♠"
    case hearts = "♥"
    case diamonds = "♦"
    case clubs = "♣"
}

For example, how can I do something like:

for suit in Suit {
    // do something with suit
    print(suit.rawValue)
}

Resulting example:

♠
♥
♦
♣

Answer

rougeExciter picture rougeExciter · Jun 10, 2014

This post is relevant here https://www.swift-studies.com/blog/2014/6/10/enumerating-enums-in-swift

Essentially the proposed solution is

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

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

for category in ProductCategory.allValues{
     //Do something
}