In Xcode 10, the Swift compiler is smart enough to:
Treat Optionals that wrap
Hashablevalues as beingHashable.Xcode >=9.4 will also treat
structs that contain allHashableproperties as also beingHashable.
Curiously, Xcode 10 treats Optionals as Hashable even if the build language is Swift 4, or even Swift 3.
Take the following code for example:
struct Foo: Hashable {
var string1: String?
var string2: String?
}
That code compiles under Xcode 10, and using the hashValue of a Foo object works as expected, even if the build language is Swift 3!
However, in Xcode 9.4, the Foo struct is not automatically Hashable unless you make the properties non-optional.
This code compiles and works as you would want it to in Xcode 9.4/Swift 4.1:
struct Foo: Hashable {
var string1: String
var string2: String
}
How can I make optionals Hashable in Xcode 9? I could certainly implement Hashable conformance for my struct myself, but creating good hashes for multi-part structures is actually kinda tricky, and I would like to not have to worry about it.
I also can't think of a straightforward way to make an Optional Hashable. (again, this is only an issue in Xcode < Xcode 10. My employer has not yet moved to Xcode 10, so I need a solution that works for Xcode 9.4)