定制测量单位

问题描述 投票:1回答:1

在iOS 10中,Apple引入了几个用于量化测量的组件。例如:

let velocity = Measurement(value: 3, unit: UnitSpeed.metersPerSecond)

虽然冗长,但好处是您可以转换为任何其他单元而不会出现容易出错的在线计算:

// before
let velocityMetersPerSecond = 3.0
let velocityKilometersPerHour = velocityMetersPerSecond * 1000 / 60

// after
let velocityKilometersPerHour = velocity.converted(to: .kilometersPerHour)

虽然Apple直接支持许多单元,但我需要一个他们不支持的单元。然而,Apple确实具有可扩展性,并且引入新度量的方法之一是扩展Unit类:

extension UnitSpeed {
  static let furlongPerFornight = 
    UnitSpeed(symbol: "fur/ftn", converter: UnitConverterLinear(coefficient: 
      201.168 / 1209600.0)
}

我需要从meters/second的源头到min/km单位的速度。下面的数学运算是转换的工作原理:

min / km = 1 / (m / s) * 1000 / 60

我遇到的麻烦是如何将源值的乘法逆(或倒数)表示为转换。这是一个错误的版本:

extension UnitSpeed {
  // still missing 1 / source value!
  static let minutesPerKilometer = UnitSpeed(symbol: "min/km",
    UnitConverterLinear(coefficient: 1000.0 / 60.0)
}
ios swift measurement
1个回答
4
投票

由于转换不是线性的,您需要创建自己的UnitConverter子类:

class UnitConverterInverse: UnitConverter {
    var coefficient: Double

    init(coefficient: Double) {
        self.coefficient = coefficient
    }

    override func baseUnitValue(fromValue value: Double) -> Double {
        return coefficient / value
    }

    override func value(fromBaseUnitValue baseUnitValue: Double) -> Double {
        return coefficient / baseUnitValue
    }
}

extension UnitSpeed {
    static let minutesPerKilometer = UnitSpeed(symbol: "min/km",
        converter: UnitConverterInverse(coefficient: 1000.0 / 60.0))
}

let velocity = Measurement(value: 60, unit: UnitSpeed.milesPerHour)

let velocity2 = velocity.converted(to: .minutesPerKilometer)

print(velocity2)
0.621371192237334 min/km
© www.soinside.com 2019 - 2024. All rights reserved.