Setting medium and long measurement symbols in Swift 3 - swift

In Swift when I create custom units I can only define one symbol. With the built in units there can be short, medium and long units. How do you set the other unit styles for a custom unit?
extension UnitEnergy {
static let footPounds = UnitEnergy(symbol: "ft-lbs", converter: UnitConverterLinear(coefficient: 1))
}
var test = Measurement<UnitEnergy>( value: 10, unit: .footPounds)
var formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "es")
formatter.unitStyle = .short
print( formatter.string(from: test))
formatter.unitStyle = .medium
print( formatter.string(from: test))
formatter.unitStyle = .long
print( formatter.string(from: test))
formatter.unitOptions = .providedUnit
formatter.unitStyle = .short
print( formatter.string(from: test))
formatter.unitStyle = .medium
print( formatter.string(from: test))
formatter.unitStyle = .long
print( formatter.string(from: test))
Output:
10 J
10 J
10 julios
10 ft-lbs
10 ft-lbs
10 ft-lbs

Short answer - you can't. The API does not provide any facility that allows you to provide different symbols for the three unit styles.
For custom units, the MeasurementFormatter only has the one symbol used when defining the custom unit.
Keep in mind that the need is for much more than just three different possible symbols for the three different unit styles. You would actually need three different string formats because some units might have a space or other punctuation, some might not. Some might appear before the value while some appear after the value.
And then there is the issue of localizing the unit. The Foundation framework provides all of this information for all supported languages so MeasurementFormatter can show all three unit styles for all supported languages for all predefined units.
Since the API does support custom units but not the ability to provide unit style specific symbols, I would suggest filing an enhancement request with Apple.

Have the same question, if there is any news please let me know. For now, I solved it like:
extension MeasurementFormatter {
func customString(from unit: Unit) -> String {
guard self.unitStyle == .long else { //I only needed .long but you get the idea
return self.string(from: unit)
}
switch unit {
case UnitEnergy.footPounds: return "foot-pounds"
default: return self.string(from: unit)
}
}

Related

What is the best way to get a Swift string into a swift-currency/USD format?

How do I get a string into a 'currency' format USD(xxx)?
I am trying to use the following library:
https://github.com/peek-travel/swift-currency
I want to be able to take a string:
var testStr1 = "$30.01"
var testStr2 = "$ 30.01"
and convert this into a currency as I have read from several posts using a double or float is bad, but if I start with a String, what else can I convert it to?
I thought I could use the "import Currency" library to do this, but this is not working.
let updatedString = testStr1.replacingOccurrences(of: "$", with: "")
let formatter = NumberFormatter()
formatter.locale = Locale.current // USA: Locale(identifier: "en_US")
formatter.numberStyle = .decimal
let number = formatter.number(from: test)
var dollars = USD(updatedString)
How do I get a string into a 'currency' format USD(xxx)? If there is a better way to accomplish this?
The basic concept presented by Himanshu works fine, but your problem isn't necessarily making use of an appropriate formatter, but how to fix your input, as the formatter expects a NSNumber and not a String.
So a quick internet check had me looking at Remove all non-numeric characters from a string in swift
So I could take a String, filter out all the "non numerical" junk and then make a Double out of it.
let input = Double(value.filter("0123456789.".contains))
from there I was able to borrow the concept from Himanshu and make a simple format function
func format(_ value: String, locale: Locale = Locale.current) -> String? {
guard let input = Double(value.filter("0123456789.".contains)) else { return nil }
//value.trimmingCharacters(in: .whitespacesAndNewlines)
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
currencyFormatter.locale = locale
return currencyFormatter.string(from: NSNumber(value: input))
}
I then made use of a Playground to test it using
var testStr1 = "$30.01"
var testStr2 = "$ 30.01"
format(testStr1, locale: Locale(identifier: "en_US")) // $30.01
format(testStr2, locale: Locale(identifier: "en_US")) // $30.01
format(testStr1, locale: Locale(identifier: "fr_FR")) // 30,01 €
format(testStr2, locale: Locale(identifier: "fr_FR")) // 30,01 €
format(testStr1, locale: Locale(identifier: "de_DE")) // 30,01 €
format(testStr2, locale: Locale(identifier: "de_DE")) // 30,01 €
Now, if you specifically want to use USD(xxx) as the format, then you could simply use a basic NumberFormatter and generate your own String from the resulting conversion of the input to a Double
I have read from several posts using a double or float is bad
So, yes, maintaining a currency value as a Double or Float is generally a bad idea, currency values are typically maintained as a Int or Long, but this is due to how Double and Float representation works in computers, for the, general, presentation, you should be fine, but each use case needs be assessed.
let currencyFormatter = NumberFormatter()
currencyFormatter.usesGroupingSeparator = true
currencyFormatter.numberStyle = .currency
// localize to your grouping and decimal separator
currencyFormatter.locale = Locale.current
// We'll force unwrap with the !, if you've got defined data you may need more
error checking
let priceString = currencyFormatter.string(from: 9999.99)!
print(priceString) // Displays $9,999.99 in the US locale
**Forcing a Custom Locale**
You can override the users locale to display specific currency formats by changing the Locale using the identifier.
currencyFormatter.locale = Locale(identifier: "fr_FR")
if let priceString = currencyFormatter.string(from: 9999.99) {
print(priceString) // Displays 9 999,99 € in the French locale
}
currencyFormatter.locale = Locale(identifier: "de_DE")
if let priceString = currencyFormatter.string(from: 9999.99) {
print(priceString) // Displays 9.999,99 € in the German locale
}

Why does the following time sorting code break for devices using a 24-hour clock?

Apologies if this is an obvious question, but I built an app with Swift a while back and periodically make updates to it, but each time I come back to it I'm pretty rusty.
I have the following code that sorts items by time:
let dateFormat = DateFormatter()
dateFormat.dateFormat = "h:mm a"
items = items.sorted(by: {dateFormat.date(from: $0.time)! < dateFormat.date(from: $1.time)!})
This works fine for devices using 12-hour time, but throws an error for devices using 24-hour time (and possibly if the locale uses 12-hour time, but not AM/PM?).
Through reading various Stack Overflow questions and developer documentation, and from some trial and error, I've found that adding the following line of code fixes the issue:
dateFormat.locale = Locale(identifier: "en_US")
If I'm sorting by time and I've specified my date format, why does it matter what locale a device is using? When the DateFormatter documentation says:
... provides a representation of a specified date that is appropriate
for a given locale.
Does that mean every date formatter must have a locale specified and since I hadn't specified one in my original code it assumed the locale of the device? Therefore, if a user was using a locale that didn't support a 12-hour clock it threw an error?
An approach...
Since you have two possible formats, you need two formatters...
let format12 = DateFormatter()
format12.dateFormat = "h:mm a"
// Fix the possible formatting and avoid issues with the local and parsing
format12.locale = Locale(identifier: "en_US_POSIX")
let format24 = DateFormatter()
format24.dateFormat = "H:mm"
format24.locale = Locale(identifier: "en_US_POSIX")
Now you need to parse your input into date values...
var items: [String] = ["1:00 PM", "4:00 PM", "13:00", "16:00"]
var dates: [Date?] = items.map {
if let value = format24.date(from: $0) { return value }
else if let value = format12.date(from: $0) { return value }
return nil
}
And sort it...
dates.sort {
guard let lhs = $0, let rhs = $1 else { return true }
return lhs < rhs
}
Now, the above anticipates that some values may be nil and takes appropriate action.
Now you could, instead, use compactMap to remove the possible nil values, for example...
var dates: [Date] = items.compactMap {
if let value = format24.date(from: $0) { return value }
else if let value = format12.date(from: $0) { return value }
return nil
}
dates.sort {
guard let lhs = $0, let rhs = $1 else { return true }
return lhs < rhs
}
dates.sort { $0 < $1 }
I dumped all of this into Playground and have no issues
Side note...
If you are getting nil values with either of the two formatters, then you need to take a closer look at the values and determine how they are not fitting the pattern of the formatters and take appropriate actions to correct it
Instead of create Date object and sort
you can create timestamp and then sort it based on timestamp.

MeasurementFormatter not working as intended

let userDefined = Measurement(value: Double(userInput.text!)!, unit: UnitMass.kilograms)
let calculatedValue = userDefined.converted(to: UnitMass.grams)
print(calculatedValue)
let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_US")
Convertedunit.text = formatter.string(from: calculatedValue)
The user input is 5.
The output of print(calculatedValue) is 5000.0g.
However, the output of Convertedunit.text is 11.003lbs which is in pounds. I tried to use different methods, but it is still not in grams. Can anyone enlighten me?
Because of formatter.locale = Locale(identifier: "en_US"), the formatter will automatically convert everything to the imperial unit system, which in this case is pounds. Grams on the other hand belong to the metric system.
There are two ways to change this behaviour:
A) If you want to use the metric system specify a locale for a country that uses it, such as Germany. formatter.locale = Locale(identifier: "de_DE"). Don't worry, this will not affect the language of the string ( such as German: Meter, English: Meters, French: Mètres) as that is still bound to the apps language.
B) If you want to keep whatever unit you put into the formatter simply declare: formatter.unitOptions = .init(arrayLiteral: .providedUnit)
That way the formatter will generate strings with whatever unit you have provided it with.

Adding additional unitOptions to MeasurementFormatter in Swift

Is it possible to add custom unitOptions to the MeasurementFormatter in swift? I would like to be able to define ones such as .imperial and .metric
extension UnitEnergy {
static let footPounds = UnitEnergy(symbol: "ft-lbs", converter: UnitConverterLinear(coefficient: 1))
}
var test = Measurement<UnitEnergy>( value: 10, unit: .footPounds)
var formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "es")
print( formatter.unitOptions )
print( formatter.string(from: test))
formatter.unitOptions = .naturalScale
print( formatter.unitOptions )
print( formatter.string(from: test))
formatter.unitOptions = .providedUnit
print( formatter.unitOptions )
print( formatter.string(from: test))
Output:
UnitOptions(rawValue: 0)
10 J
UnitOptions(rawValue: 2)
2,39 cal
UnitOptions(rawValue: 1)
10 ft-lbs
You could of course create your own class that would take imperial and metric unit options and call measurement formatter with a locale that behaves as you expect and desire. It's the simplest approach and works as you would expect. Using en_US and en_AU worked as I expected and wanted it to for this very purpose.
More information and a guide here: Introduction to MeasurementFormatter.

How to know if user prefers miles or kilometers?

In my app I've got a certain distance in meters.
And I want to display it in kilometers if user prefers kilometers and display it in miles if user prefers miles. And in the first case I want to add to a string "kilometers" at the end and in the second one to add "miles".
What is the best way to achieve this goal?
Thanks.
To determine whether the user uses metric or not, NSLocale can tell you:
- (BOOL)isMetric {
return [[[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem] boolValue];
}
Swift equivalent of Chris' answer would be something like this:
func isMetric() -> Bool {
return ((Locale.current as NSLocale).object(forKey: NSLocale.Key.usesMetricSystem) as? Bool) ?? true
}
Note that it defaults to true under certain circumstances. Change as needed.
You could ask the user whether they prefer miles or kilometers, in a preference or something. Then whenever you display a distance you would say.
In pseudo c code
function distance(meters) {
if (userPrefersKM) {
return meters / 1000 + " kilometers";
else if (userPrefersMiles) {
return meters / METERS_IN_A_MILE + " miles";
}
Where METERS_IN_A_MILE would be about 1600, but you should look that up.
In Swift, Locale.current.usesMetricSystem gives what the user would expect. But you don't need that if you use Measurement which handles it for you.
let distanceInMeters: Double = 2353.45
let formatter = MeasurementFormatter()
formatter.unitStyle = .medium // adjust according to your need
let distance = Measurement(value: distanceInMeters, unit: UnitLength.meters)
formatter.string(from: distance)
The current locale dictates how it is presented to the user. To see how it works for different locales, try this in a Xcode Playground (examples are for UK and France):
let distanceInMeters: Double = 2353.45
let formatter = MeasurementFormatter()
formatter.unitStyle = .medium // adjust according to your need
let distance = Measurement(value: distanceInMeters, unit: UnitLength.meters)
formatter.locale = Locale(identifier: "en_UK")
formatter.string(from: distance) // 1.462 mi
formatter.locale = Locale(identifier: "en_FR")
formatter.string(from: distance) // 2,353 km
Unless the iPhone provides this information directly, you'll have to have a lookup table from locale to default unit. Then you should allow the user to override that default.