Swift 3 Programming Language – Example of Enumerations

Enumeration Example

Example of Playing cards type

enum Suit {
    // Different cases for Card
    case spades, hearts, diamonds, clubs

    // Description of each type
    func simpleDescription() -> String {
        switch self {
            case .spades:
                return "Spades"
            case .hearts:
                return "Hearts"
            case .diamonds:
                return "Diamonds"
                return "Clubs"

Creating a constant of above type and printing description

// lets create a constant of type `Suit` enum
let hearts = Suit.hearts

// print description
print("Type of card is \(hearts.simpleDescription())")


Add a color() method to Suit that returns black for spades and clubs and returns red for hearts and diamonds.

// Creating an extension to achieve above
extension Suit {
    // a function that returns color for card type
    func color() -> String {
        switch self {
            case .hearts, .diamonds:
                return "Red"
                return "Black"

print("and color is \(hearts.color())")

Output for above code is as follows.

Type of card is Hearts
and color is Red

Click here to open actual swift-playground file for above illustration