How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

Info on Enumerations as dictionary keys:

From the Swift book:

Enumeration member values without associated values (as described in
Enumerations) are also hashable by default.

However, your Enumeration does have a member value with an associated value, so Hashable conformance has to be added manually by you.

Solution

The problem with your implementation, is that operator declarations in Swift must be at a global scope.

Just move:

func == (lhs: StationSelector, rhs: StationSelector) -> Bool {
    return lhs.toInt() == rhs.toInt()
}

outside the enum definition and it will work.

Check the docs for more on that.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)