Swift - Enumerations with back integer

Introduction

Enumerations can have a corresponding integer number.

Enumerations in Swift allow you to store associated values.

The associated values can be any type.

Each member of the enumeration can have a different set of values.

Demo

enum Level { 
    case LevelOne(powerLevel: Int) 
    case LevelTwo(range: Int) 
} 
let spaceLaser = Level.LevelOne(powerLevel: 5) 

switch spaceLaser { 
    case  .LevelOne(powerLevel: 0...10 ): 
        print("It's a LevelOne with power from 0 to 10!") 
    case  .LevelOne: 
        print("It's a LevelOne!") 
    case  .LevelTwo(let range): 
        print("It's a LevelTwo with range \(range)!") 
}

Result

Related Topic