首页 > 解决方案 > 用于 Greate 操作的 Swift stdlib 默认实现

问题描述

extension Decimal: LCDecimalPrecise {
public static func == (lhs: Decimal, rhs: Decimal) -> Bool {
    return !((lhs - rhs).doubleValue > LCMetricUnit.moneyPrecise ||
           (rhs - lhs).doubleValue > LCMetricUnit.moneyPrecise)
}

public static func < (lhs: Decimal, rhs: Decimal) -> Bool {
    return (lhs - rhs).doubleValue < -LCMetricUnit.moneyPrecise
}

public static func > (lhs: Decimal, rhs: Decimal) -> Bool {
    return (lhs - rhs).doubleValue > LCMetricUnit.moneyPrecise
}

}

覆盖前两个方法后(>忽略运算符方法),然后调用以下方法,例如:

 if Decimal(string: "1") > Decimal(string: "2") {//TODO sth}

我认为 less(<) 运算符将期望根据文档和源代码被调用,

调试时,它不会被调用。这么奇怪!!!最后,我们实施更大的操作来解决这个问题。

标签: swiftoverloadingoperator-keyword

解决方案


推荐阅读