GJZ GJZ - 17 days ago 8
iOS Question

Xcode : Compiler error Dictionary <key, value > is not convertible to hashable

I'm making an app in Xcode using Swift and the build fails even though there seem to be no errors in the code. I do, however, get this error:


Compiler error Dictionary <>key, value <> is not convertible to hashable.


How can I fix this? I have searched this but found nothing helpful.

Answer

ObjectiveC:
An NSDictionary key must conform to the NSCopying protocol. If the key is a custom object add the code to make it conform.

Swift:
All of a dictionary's keys must be compatible with its key type (Key).

From The Swift Programming Language - Dictionary Type:

The key type of a dictionary must conform to the Swift standard library Hashable protocol.

From Coding Explorer BLog:

The built in Swift types like Int, Double, and String are hashable.