简体   繁体   English

Swift 2.2中协议“ Equatable”的“泛型”冗余一致性

[英]Redundant conformance of 'Generic' to protocol 'Equatable' in Swift 2.2

I am Having an error in Equatable while compilation. 编译时,Equatable出现错误。 I wanted to add find or contains method in the list to check the value. 我想在列表中添加查找或包含方法以检查值。 My Code Below 我的下面的代码

class Generic: NSObject, Equatable, NSCoding //Am Having an error -  Redundant conformance of 'Generic' to protocol 'Equatable'

{

  var genericCode: String?
  var genericName : String?
  var genericType : String?
  var genericImageUrl : String?
  var genericPhone: String?
  var orgName : String?

override init()
{

    self.genericCode = String("")
    self.genericName = String("")
    self.genericType = String("")
    self.genericImageUrl = String("")
    self.genericPhone = String("")
    self.orgName = String("")

}

//Parameterzed Constructor for the Generic
init(genericCode: String , genericName: String , genericPhone: String, genericType: String, genericImageUrl : String)
{
    self.genericCode = genericCode
    self.genericName = genericName
    self.genericType = genericType
    self.genericImageUrl = genericImageUrl
    self.genericPhone = genericPhone

}

required init(coder aDecoder: NSCoder) {
    genericCode = aDecoder.decodeObjectForKey("genericCode") as? String
    genericName = aDecoder.decodeObjectForKey("genericName") as? String
    genericType = aDecoder.decodeObjectForKey("genericType") as? String
    genericPhone = aDecoder.decodeObjectForKey("genericPhone") as? String
}

func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(genericCode, forKey: "genericCode")
    aCoder.encodeObject(genericName, forKey: "genericName")
    aCoder.encodeObject(genericType, forKey: "genericType")
    aCoder.encodeObject(genericPhone, forKey: "genericPhone")
}

} }

func ==(lhs: Generic, rhs: Generic) -> Bool
{
  return lhs.genericCode == rhs.genericCode
}

Error has occured , Is it because i am using NSCoding and NSobject? 发生错误,是因为我正在使用NSCoding和NSobject吗?

A class inheriting NSObject already conforms to Equatable . 继承NSObject的类已经符合Equatable So declaring Object: Equatable is redundant. 因此,声明Object: Equatable是多余的。

However, the conformance does not mean that it has been implemented properly ( NSObject just checks whether the pointers are the same). 但是,一致性并不意味着已经正确实现了( NSObject只是检查指针是否相同)。 If you wish to properly implement Equatable for NSObject , just exclude the protocol conformance statement ( : Equatable ), but still implement the comparison method: 如果您希望为NSObject正确实现Equatable ,只需排除协议一致性声明( : Equatable ),但仍然可以实现比较方法:

static func ==(lhs: Object, rhs: Object) -> Bool {
   return lhs.text == rhs.text
}

According to the docs , NSObject conforms to Equatable : 根据文档NSObject符合Equatable

Conforms To 符合
CVarArgType CVarArgType
CustomDebugStringConvertible CustomDebugStringConvertible
CustomStringConvertible CustomStringConvertible
Equatable 平等的
Hashable 可散列
NSObjectProtocol NSObjectProtocol

... therefore, like your error says, conformance to Equatable is redundant... ...因此,就像您的错误所说,符合Equatable是多余的...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift的等同协议一致性检查 - Swift's equatable protocol conformance check Swift:Viewcontroller与协议UIGestureRecognizerDelegate的冗余一致性 - Swift : Redundant conformance of Viewcontroller to protocol UIGestureRecognizerDelegate 快速将HomePageVCNEW冗余符合协议UITableViewDelegate - swift Redundant conformance of 'HomePageVCNEW' to protocol 'UITableViewDelegate' “ NSDate”与协议“ Comparable” Swift的冗余一致性 - Redundant conformance of 'NSDate' to protocol 'Comparable' Swift 的冗余一致性 <view controller> 协议&#39;UISearchBarDelegate&#39;-Swift 4 - Redundant conformance of <view controller> to protocol 'UISearchBarDelegate' - Swift 4 当结果类型用作关联值时,Swift 枚举符合 Equatable:类型不符合 Equatable 协议 - Swift enum conformance to Equatable when result type used as associated value: Type doesn't conform to protocol Equatable ChatViewController与协议的冗余一致性 - Redundant conformance of the ChatViewController to protocol 对象与协议的冗余一致性 - Redundant conformance of Object to Protocol Swift 2通用数据结构不符合Equatable协议 - Swift 2 Generic data structure not conforming to Equatable protocol Swift协议如何声明为通用一致性 - Swift Protocol How to declare as Generic conformance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM