Create Custom Units and Dimensions in Swift (Part 2)

Learn how to use the measurements APIs to create custom units and convert units of measure easily and effortlessly with Swift.

Francesco Leoni

2 min read

Availability

iOS 10.0

iPadOS 10.0

macOS 10.12

Mac Catalyst 13.0

tvOS 10.0

watchOS 3.0

In this article we are going to lear how to create custom units and dimensions to extend the ones that Swift offers.

Creating a Custom Unit

To create a custom unit we need to use the initializer of the dimension we want to add a new unit to.

So if we want to add a new unit to UnitIlluminance, we can do the following:

let footCandleUnit = UnitIlluminance(symbol: "fc", converter: UnitConverterLinear(coefficient: 10.76))

A coefficient of '10.76' means that whenever a measurement of this unit is converted to the base unit, which is .lux, it will be multiplied by '10.76'.

Now we can convert .lux to .footCandle:

let footCandle = Measurement(value: 2, unit: footCandleUnit)

let lumen = footCandle.converted(to: .lux)

Extending an Existing Dimension

The second method uses an extension to add custom units.

extension UnitIlluminance {

static let footCandle = UnitIlluminance(symbol: "fc", converter: UnitConverterLinear(coefficient: 10.76))

}

We declare the new unit exactly as previously, passing a symbol and a coefficient. Now we can use this new unit in a slightly different way.

let footCandle = Measurement(value: 2, unit: UnitIlluminance.footCandle)

let lumen = footCandle.converted(to: .lux)

Creating a Custom Dimension

Swift allow us to create a whole new dimension by creating a subclass of Dimension.

Here is the example that Apple used in its documentation:

class CustomRadioactivityUnit: Dimension {

static let becquerel = CustomRadioactivityUnit(symbol: "Bq", UnitConverterLinear(coefficient: 1.0))

static let curie = CustomRadioactivityUnit(symbol: "Ci", UnitConverterLinear(coefficient: 3.7e10))

static let baseUnit = self.becquerel

}

  1. First you create the units of the dimension, as we've seen previously.
  2. Then you define the base unit of the new dimension using the baseUnit property.

Conclusion

As we've seen, creating custom units and dimensions is an easy task, and most of the time the built in dimensions are just what we need.

Thanks for reading.

If you have any question about this article, feel free to email me or tweet me @franceleonidev and share your opinion.

Thank you for reading and see you in the next article!

Share this article

Related articles


Units and Measurement Conversion in Swift (Part 1)

Learn how to use the measurements APIs to create custom units and convert units of measure easily and effortlessly with Swift.

2 min read

MeasurementFoundation

Persistence Controller Example

Check out this sample of the Persistence Controller with CloudKit generated by Xcode while creating a new project.

2 min read

Samples

Fix “Build input file cannot be found“ Error in Xcode

See how to fix the “Build input file cannot be found“ error in Xcode. It can be caused by different reasons.

1 min read

Q&AXcodeErrors