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

Question

What is the output of the following code?

Examine the following code snippet:

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

What is the output for the following statements?

var d = Figure.Man
print(d.rawValue)

d = Figure.Baby
print(d.rawValue)


Click to view the answer

10
1

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        // = 0
    case Baby    // = 1
    case Girl  // = 2
    case SpiderMan = 9
    case Man  // = 10
    case Superman           // = 11
    case Batman             // = 12
}

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


d = Figure.Baby/*w w  w  .ja v  a2  s . c  o  m*/
print(d.rawValue)   //prints out 1---

Result

Related Exercise