Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
777 views
in Technique[技术] by (71.8m points)

ios - Custom measurement unit

In iOS 10, Apple introduced several components for quantifying measurements. For example:

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

Although verbose, the benefits are that you can convert to any other unit without error prone in-line calculations:

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

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

While Apple supports many units straight out of the box, I have a need for a unit that they don't support. Apple did have extensibility in mind however, and one of the ways to introduce a new metric is by extending the Unit class:

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

I need the speed from the source in meters/second to units of min/km. The following math below is how the conversion works:

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

The trouble I'm having is how to express the multiplicative inverse (or reciprocal) of the source value into the conversion. Here's a erroneous version:

extension UnitSpeed {
  // still missing 1 / source value!
  static let minutesPerKilometer = UnitSpeed(symbol: "min/km",
    UnitConverterLinear(coefficient: 1000.0 / 60.0)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the conversion is not linear, you will need to create your own UnitConverter subclass:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...