Any custom type that you want to use a dictionary key must comply with the Hashable protocol.
This protocol has one property that you must implement.
var hashValue: Int { get }
Use this property to generate an int that the dictionary can use to search. You should try to make the generated hashValue unique to each pixel.
There is the following note in the Swift book, so you can make a random hash (as long as it is unique):
The value returned by a property of type hashValue does not have to be the same for different executions of the same program or in different programs.
Note that since Hashable inherits from Equatable , you must also implement:
func ==(_ lhs: Self, _ rhs: Self) -> Bool.
I'm not sure what the internal structure of your pixel is, but you can probably consider two pixels equal if both have the same x and y values. The final logic is up to you.
Change this as you need:
struct Pixel : Hashable { // MARK: Hashable var hashValue: Int { get { // Do some operations to generate a unique hash. } } } //MARK: Equatable func ==(lh: Pixel, rh: Pixel) -> Bool { return lh.x == rh.x && rh.y == lh.y }
source share