![](/img/trans.png)
[英]Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols
[英]Value of protocol type 'Any' cannot conform to 'Equatable'; only struct/enum/class types can conform to protocols
值是類型“ANY”,因為它可以是 Int 或 String。 所以無法實現 Equatable 協議。 以下是截斷的代碼。
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"
}
}
}
使用 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"
}
}
}
我有一個類似的問題,使用[AnyHashable]
而不是[Any]
類型是解決方案!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.