[英]Value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols
Value is type "ANY" as it can be Int or String.值是类型“ANY”,因为它可以是 Int 或 String。 So not able to implement Equatable protocol.所以无法实现 Equatable 协议。 following are the code snipped.以下是截断的代码。
struct BusinessDetail:Equatable {
static func == (lhs: BusinessDetail, rhs: BusinessDetail) -> Bool {
lhs.cellType == rhs.cellType && lhs.value == rhs.value
}
let cellType: BusinessDetailCellType
var value: Any?
}
enum BusinessDetailCellType:Int {
case x
case y
var textVlaue:String {
Switch self {
case x:
return "x"
case y:
return "y"
}
}
}
Use Generics instead of Any...使用 Generics 代替任何...
struct BusinessDetail<T> {
let cellType: BusinessDetailCellType
var value: T?
}
extension BusinessDetail: Equatable {
static func ==<T> (lhs: BusinessDetail<T>, rhs: BusinessDetail<T>) -> Bool {
lhs.cellType == rhs.cellType
}
static func == <T1:Equatable>(lhs: BusinessDetail<T1>, rhs: BusinessDetail<T1>) -> Bool {
lhs.cellType == rhs.cellType && lhs.value == rhs.value
}
}
enum BusinessDetailCellType:Int {
case x
case y
var textVlaue:String {
switch self {
case .x:
return "x"
case .y:
return "y"
}
}
}
I had a similar issue, where using [AnyHashable]
instead of [Any]
type was the solution!我有一个类似的问题,使用[AnyHashable]
而不是[Any]
类型是解决方案!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.