Swift 2.2, Contains Method not working
Asked Answered
P

1

4

Contains method not working properly and it is giving me false result even if it is matching with Object?

My Code Below

class Generic: NSObject, NSCoding
{

  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
}

Checking in ViewController

print(readArray.contains(generic))
if !readArray.contains(generic)
{
            readArray.append(generic)
}
Photomural answered 23/5, 2016 at 12:54 Comment(3)
Your class inherits from NSObject – you therefore need to override isEqual to do comparisons.Gratulant
Possible duplicate of Bug with equals operator and NSObjects in Swift 2.0?Gratulant
String("") and just "" is exactly the same and be aware of using optional properties and a non-optional initializer.Belgravia
M
13

OPTION 1

The reason is that your class is inheriting from NSObject, therefore, you must fulfill the NSObjectProtocol instead of Equatable:

override func isEqual(object: AnyObject?) -> Bool {
     if let object = object as? Generic {
         return self.genericCode == object.genericCode
     }
     return false
 }

OPTION 2

Inherit from Equatable instead of NSObject (you probably can't use this option, since you seem to need NSCoding)

Note that with newer versions of Swift you probably should use OPTION 3: Use Codable instead of NSCodable

Monagan answered 23/5, 2016 at 13:7 Comment(2)
That's just isEqual comparing the pointers – if you try to compare two different instances with the same genericCode, it won't work (try putting a breakpoint in the == overload, it'll never get tripped).Gratulant
XCode 10.1: override func isEqual(_ object: Any?) -> BoolLakesha

© 2022 - 2024 — McMap. All rights reserved.