A package containing measurable types for the Swift programming language

swift-measures

Measures is a package containing measurable types for the Swift programming language.

Contents

The package currently provides the following implementations:

Dimensions

AbsorbedDose
Area
CatalyticActivity
Concentration
ElectricalCapacitance
ElectricalInductance
ElectricalResistance
ElectricConductance
ElectricCurrent
ElectricPotential
ElectricTension
Energy
EquivalentDose
Force
Frequency
Illuminance
Length
LuminousIntensity
LuminousFlux
MagneticFlux
MagneticFluxDensity
Mass
Memory
Power
Pressure
Radioactivity
Speed
SubstanceAmount
ThermodynamicTemperature
Time
Volume

Objects

Measure: A representation of a measure.

  • value: The value of this measure.
  • unit: The unit of this measure.
  • init(_:_:): Creates a new instance with the specified value and unit.
  • converted(to:): Returns this measure converted to the specified unit.
  • convert(to:): Converts this measure to the specified unit.
  • isEquivalent(to:): Returns a boolean value indicating whether the two specified measures are equivalent.

Protocols

Measurable: Representing a type that can be measured.

  • coefficient: The coefficient of this instance compared to its base unit.
  • symbol: The symbol of this instance.
  • system: The measurement system of this instance.
  • init(coefficient:symbol:system:): Creates a new instance with the specified coefficient, symbol and system.
  • base: The base unit of this instance.

Relationships

Measure

  • Conforms to Addable, AdditiveArithmetic, Clampable, Codable, Comparable, CustomStringConvertible, Equatable, Hashable, Subtractable.

Measurable

  • Conforms to Codable, Comparable, Equatable, Hashable.

Installation

To use this package in a SwiftPM project:

  1. Add it to the dependencies in your Package.swift file:

let package = Package(
    ...
    dependencies: [
        .package(url: "https://github.com/alexandrehsaad/swift-measures.git", branch: "main")
    ],
    ...
)
  1. Add it as a dependency for your target in your Package.swift file:

let package = Package(
    ...
    targets: [
        .target(name: "MyTarget", dependencies: [
            .product(name: "Measures", package: "swift-measures")
        ]),
    ],
    ...
)
  1. Import the package in your source code.

import Measures

Tutorial

  1. Implement a custom measurable type:

struct FuelEfficiency: Measurable {
    let coefficient: Double
    let symbol: String
    let system: MeasureSystem
	
    init(coefficient: Double, symbol: String, system: MeasureSystem) {
        self.coefficient = coefficient
        self.symbol = symbol
        self.system = system
    }
	
    static let base: Self = .kilometrePerLitre
}
  1. Extend any measurable type:

extension FuelEfficiency {
    static let kilometrePerLitre: Self = .init(
        coefficient: Length.kilometre.coefficient / Volume.litre.coefficient,
        symbol: "Km/L",
        system: .metric
    )
    
    static let metrePerLitre: Self = .init(
	coefficient: Length.metre.coefficient / Volume.litre.coefficient,
	symbol: "m/L",
	system: .metric
    )
}
  1. Initialize and convert a measure:

var measure: Measure<FuelEfficiency> = .init(5, .kilometrePerLitre)
measure.convert(to: .metrePerLitre)

print(measure)
// Prints "5000 m/L"

Contribution

Reporting a bug

If you find a bug, please open a bug report.

Contacting the maintainers

The current code owner of this package is Alexandre H. Saad (@alexandrehsaad). You can contact him by writing an email to alexandresaad at icloud dot com.

Supporting

If you like our work, show your support by staring this repository.

Feedback

We would love to hear your thoughts or feedback on how we can improve Swift Measures!

GitHub

View Github