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)
}
Units of Measurement in Word for WindowsClick File tab on the ribbon and select Options. Go to the Advanced tab and scroll down to the Display section. Select an option from the Show measurements in units of menu. Click OK to save your selection.
A custom unit is a unit that the user defines. You can combine predefined units and custom units to create a custom system of units.
A unit of measurement is a definite magnitude of a quantity, defined and adopted by convention or by law, that is used as a standard for measurement of the same kind of quantity. Any other quantity of that kind can be expressed as a multiple of the unit of measurement.
An apple is quite small, so it should be weighed in grams. How much did you estimate that an apple weighs? A reasonable estimate would be 100 g. When we weighed an apple, it was 130 g.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With