WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

How to convert units using Unit and Measurement

Swift version: 5.6

Paul Hudson    @twostraws   

iOS 10 introduced a new system for calculating distance, length, area, volume, duration, and many more measurements. Let’s start with something simple. If you’re six feet tall, you’d create a Measurement instance like this:

let heightFeet = Measurement(value: 6, unit: UnitLength.feet)

Note that Swift can’t infer .feet to mean UnitLength.feet because there are lots of Unit subclasses as you’ll see soon.

Once you have a measurement ready, you can convert it to other units like this:

let heightInches = heightFeet.converted(to: UnitLength.inches)
let heightSensible = heightFeet.converted(to: UnitLength.meters)

You should see “72.0 in” and “1.8288 m” 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 = heightFeet.converted(to: UnitLength.astronomicalUnits)

Once you’ve used one unit, the rest work identically. Here are some more examples to get you started:

// convert degrees to radians
let degrees = Measurement(value: 180, unit: UnitAngle.degrees)
let radians = degrees.converted(to: .radians)

// convert square meters to square centimeters
let squareMeters = Measurement(value: 4, unit: UnitArea.squareMeters)
let squareCentimeters = squareMeters.converted(to: .squareCentimeters)

// convert bushels to imperial teaspoons
let bushels = Measurement(value: 6, unit: UnitVolume.bushels)
let teaspoons = bushels.converted(to: .imperialTeaspoons)

Honestly, I have no idea what the bushels to imperial teaspoons ratio is, but it’s nice to be given the option!

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Available from iOS 10.0

Similar solutions…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.8/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.