Swift For Any Measure: Simplified A clean, Swift interface for
Read my post about it here.
Requirements: Swift 4.0+
The idea of type-safe numeric values, in other words,
Measurements sounds great but, alas, the ergonomics are poor. At least until now. Wouldn't it be great to do this
let m: Mass = 123(.kilograms) + 17(.stones) // or let m = Mass(123, .kilograms) + 17(.stones) m.converted(to: .pounds) // 509.1688786398349 lb
instead of this?
let m = Measurement(value: 123, unit: UnitMass.kilograms) let m2 = m + Measurement(value: 17, unit: UnitMass.stones) print (m2.converted(to: .pounds)) // 509.1688786398349 lb
In addition to its verboseness, the Foundation Unit and Measurement APIs also lack the ability to dynamically declare compound units. This can make it difficult to perform dimensional analysis and other multi-step calculations.
Ratio structure allows you to express the ratio between two units in a type-safe manner Multiplying a measurement with one unit type by a rate whose denominator is that same unit type causes those types to cancel out, resulting in a measurement with the numerator type.
For example, volume over time multiplied by time yields volume:
// Ratio of Measures typealias FlowRate = Ratio<UnitVolume, UnitDuration> let rate: FlowRate = 84760(.cubicFeet, per: .seconds) let dailyFlow = (rate * 24(.hours)).converted(to: .megaliters) // -> ~ 207371ML
Swift Package Manager
Add the Rate package to your target dependencies in
import PackageDescription let package = Package( name: "YourProject", dependencies: [ .package( url: "https://github.com/wildthink/anymeasure", from: "1.0.0" ), ] )
Then run the
swift build command to build your project.
- Medium: Measurements in Swift
MIT - See LICENSE.md
- Flight School: Read Evaluate Press for Rate.swift Original Code at https://github.com/Flight-School/Rate This functionality is discussed in Chapter 5 of Flight School Guide to Swift Numbers.