
class Item : Printable,Hashable { var description:String { return "..." } var hashValue:Int { return 1 }} 我收到一条错误,指出类型’Item’不符合协议’Equatable’,即使我还没有尝试实现一个名为’Equatable’的协议.有没有人见过这样的行为?任何解决方案或解决方法?谢谢!
解决方法 根据 theHashable docs:(见该页面的最底部) Types that conform to the Hashable protocol must provIDe a gettable Int property called hashValue,and must also provIDe an implementation of the “is equal” operator (==).
根据the 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是你认为会使它们等效的东西.
总结以上是内存溢出为你收集整理的ios – Type不符合未定义的协议全部内容,希望文章能够帮你解决ios – Type不符合未定义的协议所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)