How to create an enumeration of constants with and without explicit values in Swift

1 Answer

0 votes
/* 
   Title: Enumeration of Constants in Swift
   Example with and without explicit values
*/

import Foundation

// Enum WITHOUT explicit values
enum Color: Int {
    case red      // 0
    case green    // 1
    case blue     // 2
}

// Enum WITH explicit values
enum Status: Int {
    case ok = 1
    case warning = 5
    case error = 6
    case critical = 10
}

print("Enum without explicit values:")
print("red =", Color.red.rawValue)
print("green =", Color.green.rawValue)
print("blue =", Color.blue.rawValue)

print("\nEnum with explicit values:")
print("ok =", Status.ok.rawValue)
print("warning =", Status.warning.rawValue)
print("error =", Status.error.rawValue)
print("critical =", Status.critical.rawValue)


/* 
run:

Enum without explicit values:
red = 0
green = 1
blue = 2

Enum with explicit values:
ok = 1
warning = 5
error = 6
critical = 10

*/

 



answered Apr 26 by avibootz

Related questions

...