NSMeasurementFormatter reversed


Gerriet M. Denkmann
 

NSMeasurementFormatter is a magical tool which converts all stuff just into the right form the user wants to see.

But what about the other way round?

I have a TextField labeled “Desired Temperature” the user enters “83.4” - what does the user want?
Boiling hot (using Celsius) or comfortably warm (Fahrenheit)?

So my app does:

if ( NSLocale.currentLocale.usesCelsius ) ….

The problem: I can’t find such a property.

Or:
NSMeasurement *raw = [measurementFormatter measurementFromString: “83.4” for: NSUnitTemperature];
NSMeasurement *m = [raw measurementByConvertingToUnit: NSUnitTemperature.celsius];

No luck either.

So:

NSMeasurement *trySome = [[NSMeasurement alloc] initWithDoubleValue: 83.4 unit: NSUnitTemperature.celsius ];
NSString *trialResult = [measurementFormatter stringFromMeasurement: trySome];

BOOL usesCelsius = …trialResult resembles somehow “83.4” …;
This is rather error prone; what if the actual user-input was 83.399999999 ?

There clearly must be a much better way.

Gerriet.

Join {cocoa@apple-dev.groups.io to automatically receive all group messages.