I have something like this:
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:[AnyObject.Type:Dictionary] = [
Lumber.Type: lumberSize,
Fruit.Type: fruitSize
]
On my size
dictionary definition, I get this real-time error from Xcode Editor:
Type 'AnyObject.Type' does not conform to protocol 'Hashable'
How do I accomplish what I am trying to do with size
? That is, how do I create a dictionary linking types to their specific size dictionaries?
I am thinking that ObjectIdentifier
would help me since it is Hashable
but I do not know how to use it, or if it is the right choice.
Hashable
is a protocol thatObjectIdentifier
implements. This meansObjectIdentifier(Lumber.Type)
is hashable, not thatLumber.Type
is. You could try changing your code to use ObjectIdentifier, as in:This compiles and prints "2x4", but I'm not sure if it meets your specific needs. Personally, I would just use the string version of the class name as the key -
String(Lumber)
. i.e.: