Skip to content

Instantly share code, notes, and snippets.

@pookjw
Last active April 8, 2019 21:23
Show Gist options
  • Save pookjw/03e59ac9bb5e6d0f20e22322c2000eda to your computer and use it in GitHub Desktop.
Save pookjw/03e59ac9bb5e6d0f20e22322c2000eda to your computer and use it in GitHub Desktop.
understanding enum:Int in Swift 5

I got a curious about how compiler determines a value's rawValue. First, I give you an example code:

enum Rank: Int{
    case Tom
    case Jane
    case Steve, Tim
}

In this situation, Rank.Tom.rawValue is 0, also Rank.Jane.rawValue is equal to 1 (increment as 1). It means, compiler determines rawValue automatically starting from 0, if data type is Int.

Let me give you an another example, I decided another starting rawValue point as 5 like below:

enum Rank: Int{
    case Tom = 5
    case Jane
    case Steve, Tim
}

Then Rank.Jane.rawValue is equal to 6. Also, If I set Jane's rawValue as 10 (keeping Tom = 5), Rank.Steve.rawValue is equal to 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment