#31: Enum Raw Values
Question
// Swift Quiz
— objc.io (@objcio) September 25, 2019
enum Skill: Int {
case swiftUI, combine, arkit = 3
static subscript(n: Int) -> Skill? {
return Skill(rawValue: n)
}
}
let skill = Skill[2]
// What’s the value of skill?
Solution
Solution: nil
— objc.io (@objcio) September 26, 2019
By default, the compiler gives the first case the raw value 0, and the second case has a raw value of 1.
Here, we only override the third case to have value 3. That means there's no case with the raw value 2, hence the nil result.https://t.co/s7500CWmAg