In this Swift programming tutorial, you will learn about the Measurement struct and how to use it to convert one unit from another unit of the same type.
You may need to convert one unit to another while developing an iOS app. It can be done by using a unit conversion formula. We can do it by ourselves if we know the formula of conversion. But doing this can be complicated and we need to know the formula.
Luckily there is a built-in structure
Measurement provided by Apple that can help us with conversion between units.
It can help us to convert the unit in an effective way. We have to write less amount of code. The interesting part is that we don’t even need to know the formula.
For example, you can convert temperature units from Fahrenheit to Celcius and vice versa or from Celcius to kelvin and vice versa and so on for length, area, volume, etc using the
Measurement struct. But it has to be the same type. For example, You can not perform the conversion between a unit of length and a unit of volume.
Measurement is part of the Foundation framework of Swift. So you must import the Foundation framework before using the
Below is given the syntax to create an instance of the
Measurement struct in Swift:
let myUnitValue = Measurement(value: DOUBLE_VALUE, unit: UNIT_TYPE.UNIT)
In the above syntax,
myUnitValue is the variable or in this case the name of the constant to store the instance of the Measurement unit type.
DOUBLE_VALUE is the value of the unit in type double.
UNIT_TYPE is the type of unit and
UNIT is the unit of Measurement.
Let’s first start with a simple example.
Suppose the height of a wall is 8 feet. You can create an instance of Measurement for this height with type length:
import Foundation let wallHeightInFeet = Measurement(value: 8, unit: UnitLength.feet)
In the above program, the
UnitLength.feet is the unit for length. You can’t write
.feet here as there are other subclasses. The
UnitLength class has many other units for length.
To convert the above unit into inches below is the given code:
import Foundation let wallHeightInInches = wallHeightInFeet.converted(to: UnitLength.inches)
Below we are converting the unit of length to other units of length from feet:
import Foundation let wallHeightInFeet = Measurement(value: 8, unit: UnitLength.feet) let wallHeightInMeters = wallHeightInFeet.converted(to: UnitLength.meters) let wallHeightInKilometers = wallHeightInFeet.converted(to: UnitLength.kilometers) let wallHeightInCentimeters = wallHeightInFeet.converted(to: UnitLength.centimeters) let wallHeightInMillimeters = wallHeightInFeet.converted(to: UnitLength.millimeters)
Below is another example where we are converting the gram unit to other units of mss.
import Foundation let weightInKg = Measurement(value: 13, unit: UnitMass.kilograms) let weightInGram = weightInKg.converted(to: UnitMass.grams) let weightInMilligram = weightInKg.converted(to: UnitMass.milligrams) let weightInDecigram = weightInKg.converted(to: UnitMass.decigrams)
In the same way, you can perform the conversion task between other units. Here is an example of converting Fahrenheit to Celsius and vice versa using the Measurement.
Now let’s look at some interesting facts about Swift Measurement that are useful for app developers:
==, !=, <, <=, > and >=