Michel Michel -4 years ago 131
iOS Question

Tuple comparison not working as expected

After realizing that Swift does not know how to compare pairs of numbers (made as tuples of Int). I have defined a class with the following code:

import Foundation

class NumberPair: NSObject {
var row,column:Int!

init(_ theRow: Int, _ theCol: Int) {
row = theRow
column = theCol
super.init()
}

convenience override init() {
self.init(0,0)
}

static func == (lhs: NumberPair, rhs: NumberPair) -> Bool {
return (lhs.row == rhs.row) && (lhs.column == rhs.column)
}

static func != (lhs: NumberPair, rhs: NumberPair) -> Bool {
return (lhs.row != rhs.row) || (lhs.column != rhs.column)
}
}


Then when using the class I have something like this:

var firstArray = [NumberPair](), secondArray = [NumberPair]()
...........................
// Some code working fine that fills up firstArray and secondArray.
...........................

for pairItem in firstArray {
if secondArray.index(of: pairItem) != nil {
print("We found an item that is in both arrays.")
break
}
}


Now my question is this. The code a bove does not work. I mean, eventhough I am certain the 2 arrays firstArray and secondArray contains common pairs of numbers, the message never gets printed. What is wrong? Am I missing something obvious? Any hint will be very much appreciated.

Answer Source

Try below method if you really want to inherit NSObject.

    class NumberPair: NSObject {
    public var row,column:Int!

    init(_ theRow: Int, _ theCol: Int) {
        row = theRow
        column = theCol
        super.init()
    }

    override func isEqual(_ object: Any?) -> Bool {
        guard let comparingObject = object as? NumberPair else {
            return false
        }
        return row == comparingObject.row && column == comparingObject.column
    }
}

Either above, or just drop inheritance from NSObject and conform to Equatable protocol.

class NumberPair: Equatable {
    public var row,column:Int!

    init(_ theRow: Int, _ theCol: Int) {
        row = theRow
        column = theCol
        //super.init()
    }



    static func == (lhs: NumberPair, rhs: NumberPair) -> Bool {
        return (lhs.row == rhs.row) && (lhs.column == rhs.column)
    }

    static func != (lhs: NumberPair, rhs: NumberPair) -> Bool {
        return (lhs.row != rhs.row) || (lhs.column != rhs.column)
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download