Here is a simple case example of AnyHashable not supporting casting with enums.
enum testEnum: String {
case Test
}
let myObject: AnyHashable = testEnum.Test as AnyHashable
let newObject = myObject as? testEnum
In this case newObject will return nil. If I cast instead do
let newObject = (myObject as? AnyObject) as? testEnum
it will cast fine.
I have tried this with structs, custom classes and String and they all cast properly. For instance this works.
let myObject: AnyHashable = "Test" as AnyHashable
let newObject = myObject as? String
Is this a bug in swift or am I just not doing this correctly here.
I tried this in Swift 3.2 and Swift 4.
AnyHashable
explicitly type-erases:
The AnyHashable type forwards equality comparisons and hashing operations to an underlying hashable value, hiding its specific underlying type.
The fact that this happens to work for some types is the surprising fact, not that it fails for enums. I would expect it's a performance optimization to help with dictionaries. But this isn't how you're expected to use AnyHashable
. The expected usage is to initialize it with AnyHashable.init
, not as AnyHashable
.
What you meant is this:
enum TestEnum: String {
case test
}
let myObject = AnyHashable(TestEnum.test)
myObject.base // => test (as an Any)
myObject.base as? TestEnum // => Optional<TestEnum>(.test)
Note that AnyHashable
is a completely different kind of thing than Any
or AnyObject
. The latter two are protocols. The former is a type-erasing struct. The prefix Any...
in the stdlib means "type-eraser" except for the oddball special-cases (that you should avoid as much as humanly possible) Any
and AnyObject
.