Swift - What is the output: enumeration raw type auto increment?

Question

What is the output of the following code?

Examine the following code snippet:

enum Figure: Int {
   case FelixTheCat = 1
   case Baby
   case AMan
   case SpiderMan = 9
   case Girl
   case Superman
   case Batman
}


What is the output for the following statements?
var d = Figure.Girl
print(d.rawValue)

d = Figure.Baby
print(d.rawValue)


Click to view the answer

10
2

Note

The output for the following statements is as follows.

The statements in bold are the values implicitly assigned by the compiler.

Demo

enum Figure: Int {
    case FelixTheCat = 1
    case Baby    // = 2
    case AMan  // = 3
    case SpiderMan = 9                                 
    case Girl// = 10
    case Superman           // = 11
    case Batman             // = 12
}

var d = Figure.Girl
print(d.rawValue)   //prints out 10

d = Figure.Baby/*from  w  ww.j a v  a 2s .c  om*/
print(d.rawValue)   //prints out 2

Result

Related Exercise