A package containing a set of objects and protocols to create and convert units of measure, for the Swift programming language.
No overview available.
- iOS 13.0+
- iPadOS 13.0+
- macCatalyst 15.0+
- macOS 10.15+
- watchOS 6.0+
The Swift Package Manager is a tool for managing the distribution of Swift code and is integrated into the swift compiler.
-
Add the package to the dependencies in your
Package.swift
file.let package: Package = .init( ... dependencies: [ .package(url: "https://github.com/bikecalc/swift-measures.git", branch: "main") ], ... )
-
Add the package as a dependency on your target in your
Package.swift
file.let package: Package = .init( ... targets: [ .target(name: "MyTarget", dependencies: [ .product(name: "Measures", package: "swift-measures") ]), ], ... )
-
Import the package in your source code.
import Measures
-
Create a dimension that conforms to the measurable protocol.
struct FuelEfficiency: Measurable { let coefficient: Double let constant: Double let symbol: String init( coefficient: Double, constant: Double = 0, symbol: String ) { self.coefficient = coefficient self.constant = constant self.symbol = symbol } static let base: Self = .kilometresPerLiter }
-
Create a minimum of two units.
extension FuelEfficiency { static let kilometresPerLiter: Self = .init( coefficient: Length.kilometre.coefficient / Volume.liter.coefficient, symbol: "Km/L" ) static let metresPerLiter: Self = .init( coefficient: Length.metre.coefficient / Volume.liter.coefficient, symbol: "m/L" ) }
-
Initialize a measure of your dimension with a value and unit.
var measure: Measure<FuelEfficiency> = .init(5, .kilometresPerLiter)
-
Convert the measure to your other unit.
measure.convert(to: .metresPerLiter) print(measure) // Prints "5000m/L"
You can read more about this package by visiting the documentation page.
Contributions are what makes the open source community such an amazing place to learn, inspire, and create. If you wish to contribute and be part of this project, please fork the repository and create a pull request.
- Fork the repository
- Create your feature branch
git checkout -b feature/NewFeature
- Commit your changes
git commit -m 'Added a new feature'
- Push to your branch
git push origin feature/NewFeature
- Open a pull request
If you find a bug, please create an issue.
If you want to share your thoughts on how to improve this repository, you can contact the code owner. See the CODEOWNERS.txt
file for more information.
If this repository has been useful to you in some way, show your support by starring it.
To be a truly great community, we welcome developers from all walks of life, with different backgrounds, and with a wide range of experience. A diverse and friendly community will have more great ideas, more unique perspectives, and produce more great code. We will work diligently to make this community welcoming to everyone. See the CODEOFCONDUCT.md
file for more information.
Distributed under Apache License v2.0 with Runtime Library Exception. See the LICENSE.md
file for more information.