Clicky

iOS Dev Nugget 203 Swift Measurement

.

Need to run a code review on your codebase? Hire me

Swift provides a Measurement type that represents a value and its unit. e.g. 2cm.

var v1 = Measurement(value: 1, unit: UnitLength.inches) //1"
v1.convert(to: UnitLength.centimeters) //v1 is now in cm
var v2 = Measurement(value: 2, unit: UnitLength.inches) //2"
v1+v2 //addition

Measurements helps introduce type safety into calculations and reduces conversion errors.


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues