I came across a heterogeneous dictionary definition like this on a tutorial online:
var mixedMap4 = [AnyHashable(0): "Zero" as Any,
AnyHashable(1): 1.0 as Any,
AnyHashable("pi"): 3.14 as Any]
I was wondering why the author chose to write
AnyHashable(0)
instead of 0 as AnyHashable
. When I tried this on Swift playground, it also worked. However when I turned "Zero" as Any
into Any(0)
it gives the following
error: error: The Dictionary.xcplaygroundpage:41:34: error: protocol type 'Any' cannot be instantiated var mixedMap4 = [AnyHashable(0): Any("Zero") ,
Thank you for the answer