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.

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

Most developers are not aware that Apple introduced the units and measurements APIs. They are part of the Foundation framework and available on every Apple platform.

Let's get started by seeing what Unit, Dimension, and Measurement are.

Unit

  • The Unit class represents a unit that has a string to describe it called symbol.

Dimension

  • The Dimension class inherits from Unit.
  • It has multiple subclasses, each representing a different dimension (e.g. UnitAcceleration, UnitDuration, and UnitLength).
  • Each of these subclasses defines a base unit and multiple additional units that can be converted to other units of the same dimension by using a UnitConverter.
  • A list of all dimension subclasses can be found in Apple's documentation.

Measurement

  • The Measurement struct wraps a double value together with a unit.

Usage

Now lets see how Measurement works.

If you're 1,73 meters tall, you'd create a Measurement instance like this:

let heightInMeters = Measurement(value: 1.73, unit: UnitLength.meters)

Then you can convert it to other units like this:

let heightInInches = heightInMeters.converted(to: UnitLength.inches)

You should see “28.7 in” in your output, showing that the conversion process has worked.

The UnitLength class, like all unit subclasses, spans a huge range of units from old to futuristic. For example, you can convert feet to astronomical units, which is equal to the average distance between the Earth and the Sun, or about 150 million kilometers:

let heightAUs = heightInFeet.converted(to: UnitLength.astronomicalUnits)

Every unit works exactly the same as the one we've seen. Here are some more examples:

// Convert fahrenheit to celsius

let fahrenheit = Measurement(value: 6, unit: UnitTemperature.fahrenheit)

let celsius = fahrenheit.converted(to: .celsius)

// Convert kilometersPerHour to milesPerHour

let kilometersPerHour = Measurement(value: 35, unit: UnitSpeed.kilometersPerHour)

let milesPerHour = kilometersPerHour.converted(to: .milesPerHour)

Conclusion

As we have seen Swift provides an easy-to-use and powerful way of defining, using, and converting measurements.

Since there are currently 21 built-in dimensions with about 200 units combined, this will be enough for most apps. But there is also the possibility to define and add special units or even whole new dimensions. We will cover this in the next article.

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


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.

2 min read

MeasurementFoundation

How to Correctly use .secondary Hierarchy in SwiftUI

Discover how to apply hierarchical styling to text, buttons, shapes, images and labels. Improve your UI and UX with this styling.

1 min read

SwiftUIText

Read and Write Data in a Sandboxed App (Part 1)

Discover how to get read and write permission to user's Mac folders and files in an app with Sandbox enabled.

4 min read

MacOSFile system