Reputation: 5529
I'm aware of creating an Enum
from a UInt
using rawValue:
. However, can Swift cast a UInt
to an Enum
using as
? Here's a screenshot from the WWDC 2016 talk Delivering an Exceptional Audio Experience:
As you can see, the code retrieves a numeric value from the dictionary and casts it to an Enum
using as!
.
I haven't been able to do this in my code. Is it valid Swift?
Upvotes: 0
Views: 207
Reputation: 130172
I have been trying to reproduce the code with the same enum and with my own enums and the cast always crashes as it should, in my opinion, e.g.:
let x: AVAudioSessionInterruptionType = .began
let dictionary: NSDictionary = [AVAudioSessionInterruptionTypeKey: x.rawValue as NSNumber]
// crash
let value = dictionary[AVAudioSessionInterruptionTypeKey] as! AVAudioSessionInterruptionType
I went through the Swift evolution list and I haven't found a change that could cause this to work. To be honest, not all code snippets shown in WWDC are working.
Upvotes: 1