Type does not conform to undefined protocol

In Xcode 6 Beta 2, I wrote the following class:

class Item : Printable, Hashable {
    var description:String {
     return "..."
    }
    var hashValue:Int {
        return 1
    }
}

I get an error indicating that the Type 'Item' does not comply with the Equatable protocol, although I have not tried to implement a protocol called Equatable. Has anyone seen this behavior? Any solution or workaround? thank!

+4
source share
2 answers

According to Hashabledocs: (see the bottom of this page)

Types that conform to the Hashable protocol must contain the gettable Int property, called hashValue, and must also provide the implementation of the equal operator (==).

Equatable docs , ==, .

func == (lhs: MyStruct, rhs: MyStruct) -> Bool {
    return lhs.name == rhs.name
}

, :

class Item : Printable, Hashable {
    var description:String {
        return "..."
    }
    var hashValue:Int {
        return 1
    }
}

func == (lhs: Item, rhs: Item) -> Bool {
    return lhs.hashValue == rhs.hashValue
}

// Testing...
Item() == Item() //-> true

, hashValue - , , , , .

+4

Hashable Equatable, ,

+2

Source: https://habr.com/ru/post/1547805/


All Articles