Redundant conformance of 'Generic' to protocol 'Equatable' in Swift 2.2
Asked Answered
M

2

13

I am Having an error in Equatable while compilation. 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?

Maletta answered 23/5, 2016 at 11:47 Comment(1)
NSObject is already Equatable in Swift2, so you can remove the protocol declaration: https://mcmap.net/q/161779/-swift-equatable-protocolCampestral
S
16

According to the docs, NSObject conforms to Equatable:

Conforms To
CVarArgType
CustomDebugStringConvertible
CustomStringConvertible
Equatable
Hashable
NSObjectProtocol

... therefore, like your error says, conformance to Equatable is redundant...

Schriever answered 23/5, 2016 at 11:55 Comment(2)
Have removed the error but not checking with Contains method? Doing code like below .... if !readArray.contains(generic) { readArray.append(generic) } Am i doing something wronge here?Maletta
#37392080 ... Have added another callMaletta
B
31

A class inheriting NSObject already conforms to Equatable. So declaring Object: Equatable is redundant.

However, the conformance does not mean that it has been implemented properly (NSObject just checks whether the pointers are the same). If you wish to properly implement Equatable for NSObject, just exclude the protocol conformance statement (: Equatable), but still implement the comparison method:

static func ==(lhs: Object, rhs: Object) -> Bool {
   return lhs.text == rhs.text
}
Byrne answered 21/2, 2018 at 20:16 Comment(2)
Can I override isEqual() instead of the static func == and still use == to compare?Rhomb
@Rhomb yes, tested it in a PlaygroundByrne
S
16

According to the docs, NSObject conforms to Equatable:

Conforms To
CVarArgType
CustomDebugStringConvertible
CustomStringConvertible
Equatable
Hashable
NSObjectProtocol

... therefore, like your error says, conformance to Equatable is redundant...

Schriever answered 23/5, 2016 at 11:55 Comment(2)
Have removed the error but not checking with Contains method? Doing code like below .... if !readArray.contains(generic) { readArray.append(generic) } Am i doing something wronge here?Maletta
#37392080 ... Have added another callMaletta

© 2022 - 2024 — McMap. All rights reserved.