Kodeco Forums

iOS 10 Screencast: Measurement Formatter

Formatting measurements for every language and country is hard, so Foundation includes a new MeasurementFormatter class. See how easy it is to use in the screencast.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4953-ios-10-measurement-formatter
1 Like

Genius, Sam, thank you!

But now, could you please explain what “covariance” means because each time I’ve tried to read up about it in different contexts, I’ve finished as baffled as when I began.

1 Like

Thanks @keith27

Covariance is one of those terms that’s overly confusing, and people who like to wang on about Swift like to shout about a lot.

At its simplest, it’s not that difficult to understand. Covariance describes a situation where you can treat a subtype as if it were one of its supertypes. For example, when you override a method or property in a subclass then its return type is covariant - you’re allowed to return a subtype of the original return type in the overridden method.

The problem comes in the fact that in Swift, generic are invariant by default. That means that in this case although UnitTemperature is a subtype of Unit, Measurement<UnitTemperature> cannot be used in place of Measurement<Unit>.

I hope that goes a little way to explaining it. For more details, and a far better explanation, check out Mike Ash’s Friday Q&A on the topic:

https://www.mikeash.com/pyblog/friday-qa-2015-11-20-covariance-and-contravariance.html

sam

Thanks Sam, that was a very helpful explanation and link.

1 Like

@samdavies this was funny and informative. I have a better understanding of MeasurementFormatter class. Thank you.

1 Like

@d_williams - glad you enjoyed it :slight_smile:

What about height and weight for people? I’m not finding the equivalent of isForPersonHeightUse and isForPersonMassUse

@gargoyle

As far as I’m aware, MeasurementFormatter doesn’t support this, so you might have to resort to MassFormatter and LengthFormatter for the time being. Probably worth filing a bug report to get those properties transitioned in the same way that the temperature is supported without a unit.

sam

Bug 28450109 filed. Everyone else go file the same bug now so they fix it :wink:

1 Like