Search code examples
swiftcomparisonnsdecimalnumber

Should I use isEqualTo method rather == operator to compare NSDecimalNumber


I need to compare two NSDecimalNumbers in an iOS project. Should I use method isEqualTo to compare the numbers rather using == operator? Which is considered as correct?

let num1 = NSDecimalNumber(integerLiteral: 100)
let num2 = NSDecimalNumber(integerLiteral: 100)

if num1 == num2 {
    print("equal")
} else {
    print("not equal")
}

if num1.isEqual(to: num2) {
    print("equal")
} else {
    print("not equal")
}

Solution

  • It will work exactly the same, you can override isEqual method, I think you might check this answer, it is related to Double numbers https://stackoverflow.com/a/50875275/13458946