I have following code in Playgraound which doesn't work as I wish.
class Obj: NSObject {
var va1: String? = nil
var va2: Int? = nil
init(_ v1: String,_ v2: Int){
va1 = v1
va2 = v2
}
static func ==(lhs: Obj, rhs: Obj) -> Bool {
guard lhs.va1 == rhs.va1 else { return false }
guard lhs.va2 == rhs.va2 else { return false }
return true
}
}
var objArray1: [Obj] = [Obj("a", 1), Obj("b", 2), Obj("c", 3)]
var objArray2: [Obj] = [Obj("a", 1), Obj("b", 2), Obj("d", 4)]
objArray1 += objArray2
objArray1 = Array(Set(objArray1))
But the objArray1 will contains all duplicated items.
I have check other questions like :
Removing Duplicates From Array of Custom Objects Swift .
Remove duplicate objects in an array .
However, in my case, I can't change Obj's superclass NSObject. The custom type must be a subclass of NSObject.
Question: Is there any elegant way to achieve merge two custom type array and remove duplicate custom type items