Hashable is a protocol that implements an ObjectIdentifier . This means that ObjectIdentifier(Lumber.Type) is a hashable, not Lumber.Type . You can try changing your code to use an ObjectIdentifier, as in:
class Lumber { } class Fruit { } enum Size { case small case medium case large } let lumberSize = [ Size.small: "2x4", Size.medium: "4x6", Size.large: "6x10" ] let fruitSize = [ Size.small: "grape", Size.medium: "apple", Size.large: "watermelon" ] let size:[ObjectIdentifier:[Size:String]] = [ ObjectIdentifier(Lumber.self): lumberSize, ObjectIdentifier(Fruit.self): fruitSize ] let t = size[ObjectIdentifier(Lumber.self)] let s = t?[.small] print(s ?? "no s?")
It compiles and prints "2x4", but I'm not sure if it fits your specific needs. Personally, I would just use the string version of the class name as the key - String(Lumber) . i.e:
let size:[String:[Size:String]] = [ String(describing:Lumber.self): lumberSize, String(describing:Fruit.self): fruitSize ] let t = size[String(describing:Lumber.self)] let s = t?[.small] print(s ?? "no s?")
source share