Quick Measurement Conversion

This is a quick writeup on a delightful little gem I found While creating a calculation app. Inside Foundation framework with iOS 10 there is now a structure called Measurement, which takes a value and a unit. The units come from a whole host of categories such as volume, length, mass, and many more. As I come from an engineering background, this fills me with joy. I’m not even sure how often, I will use it, but it just makes me happy. So here’s how you can use it!

Enough Already, Just get to it!

Say you want to convert feet to inches. Well that can be done as simple as this:

let feet = Measurement(value: 5, unit: UnitLength.feet)
let inches = feet.converted(to: .inches)

Well that was easy, perhaps you want to know what 1g on earth is?

let acceleration = Measurement(value: 1, unit: UnitAcceleration.gravity)
let metersPerSecondSquared = acceleration.converted(to: .metersPerSecondSquared)

I can keep going all day with this, but hope this last example demonstrates all the units that are available. There are literally tons. Just type ‘Unit’ and let autocomplete guide you.


About Joe Hoffman

I am An Electrical Engineer that spends a lot of my off time doing web development and other programming. Currently I am trying to expand my knowledge with iOS and I find that writing tutorials helps me learn more thoroughly as well as provide some useful info to others.

Speak Your Mind


Privacy Policy