The best way to deal with Measurements and Units in Swift.
Measurements and Units are newly introduced in iOS 10.
- before
Measurement<UnitLength>(value: 3, unit: .kilometers) + Measurement<UnitLength>(value: 4, unit: .kilometers)
- after
3.kilometers + 4.kilometers
SwiftMeasurement supports all units declared in Foundation framework.
- UnitLength
- UnitFuelEfficiency
- UnitElectricResistance
- UnitIlluminance
- UnitDispersion
- UnitElectricCharge
- UnitVolume
- UnitEnergy
- UnitAngle
- UnitDuration
- UnitTemperature
- UnitFrequency
- UnitPower
- UnitElectricCurrent
- UnitElectricPotentialDifference
- UnitAcceleration
- UnitSpeed
- UnitArea
- UnitMass
- UnitConcentrationMass
- UnitPressure
Current version is compatible with:
- Swift 5.0+
- masOS 10.12 or later
- iOS 10.0 or later
- watchOS 3.0 or later
- tvOS 10.0 or later
SwiftMeasurement supports multiple methods for installing the library in a project.
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like SwiftMeasurement in your projects. You can install it with the following command:
$ gem install cocoapods
To integrate SwiftMeasurement into your Xcode project using CocoaPods, specify it in your Podfile
:
pod 'SwiftMeasurement'
Then, run the following command:
$ pod install
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate SwiftMeasurement into your Xcode project using Carthage, specify it in your Cartfile
:
github "ken0nek/SwiftMeasurement"
Run carthage
to build the framework and drag the built SwiftMeasurement.framework
into your Xcode project.
- Describe relations between dimentions. [L, M, T, L/T, L/T^2, etc...]
// Speed * Duration -> Length
func * (lhs: Measurement<UnitSpeed>, rhs: Measurement<UnitDuration>) -> Measurement<UnitLength> {
let v = lhs.converted(to: .baseUnit()).value * rhs.converted(to: .baseUnit()).value
return Measurement<UnitLength>(value: v, unit: .baseUnit())
}
let duration = 10.hours // 10.0 hr
let speed = 5.kilometersPerHour // 5.0 km/h
(speed * duration).converted(to: .kilometers) // 50.00004 km