简体   繁体   English

协议类型“Any”的值不能符合“Equatable”; 只有结构/枚举/类类型可以符合协议

[英]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.

相关问题 协议类型“Encodable”的值不能符合“Encodable”; 只有结构/枚举/类类型可以符合协议 - Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols 类型 &#39;()&#39; 不能符合 &#39;View&#39;; 只有 struct/enum/class 类型才能符合协议; 使用 SwiftUI 调用函数 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols; calling functions with SwiftUI navigationBarItems“类型[视图]不能符合'视图'; 只有结构/枚举/类类型可以符合协议” - navigationBarItems “Type [view] cannot conform to 'View'; only struct/enum/class types can conform to protocols” 类型 '()' 不能符合 'View'; 只有结构/枚举/类类型可以符合协议 - Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols Swift 错误:“类型 &#39;()&#39; 不能符合 &#39;View&#39;;只有 struct/enum/class 类型可以符合协议”调用函数写入文本时 - Swift error: "Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols" when calling function to write text 我收到错误“类型'(UITextRange)-&gt;字符串?' 不能符合“BinaryInteger”; 只有结构/枚举/类类型可以符合协议” - I got error “Type '(UITextRange) -> String?' cannot conform to 'BinaryInteger'; only struct/enum/class types can conform to protocol” 协议类型不能符合协议,因为只有具体类型才能符合协议 - Protocol type cannot conform to protocol because only concrete types can conform to protocols swift 将字典转换为 jsonString 错误:协议类型 'Any' 不能符合 'Encodable' 因为只有具体类型才能符合协议 - swift Convert dictionary to jsonString error : Protocol type 'Any' cannot conform to 'Encodable' because only concrete types can conform to protocols 协议类型“Any”的值不能符合“Equatable”Swift - Value of protocol type 'Any' cannot conform to 'Equatable' Swift 尝试符合可等式通用集时,类型“Any”不符合协议“Equatable” - Type 'Any' does not conform to protocol 'Equatable' when trying to conform to an equatable generic Set
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM