How to check NSDecimalNumber.zero () in Swift

Use 'num1' as a comparison reference to determine if num2 or num3 are zero. Did it successfully in Objective-C, and I'm trying to do it in Swift:

let num1: NSDecimalNumber = NSDecimalNumber.zero()
let num2: NSDecimalNumber = NSDecimalNumber.decimalNumberWithString("0")
let num3: NSDecimalNumber = NSDecimalNumber.decimalNumberWithString("0.000001")

if num1.compare(num2) == NSOrderedSame {
    println("They match")
}

This attempt results in: "error: use of unresolved identifier" NSOrderedSame "". What is the correct way to achieve this in Swift? (Look for the book and the web, but something is missing, perhaps the obvious, thanks).

+4
source share
3 answers

Do you mean: num1.compare(num2) == NSComparisonResult.OrderedSame

+11
source

in Swift, which could be simpler, for example, this:

    if num1 == num2 {
        println("They match")
    }

BACKGROUND

( Swift Cocoa Objective-C API, " " )

Swift Objective-C Swift == ===. Swift == , NSObject. Swift isEqual:, NSObject.

+6

If you often work with NSDecimalNumber, I will connect my own operator overloads here: https://gist.github.com/anisoptera/5708afc32454ed868363

Takes care of this annoying obscure comparison of "NSOrdered ..." and even allows you to do crazy things like adding two NSDecimalNumberusing the + operator!

+2
source

Source: https://habr.com/ru/post/1544599/


All Articles