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.