Pregunta

recently it came to me that using NSNumberFormatter set to "OS X 10.0 Behavior" might be no longer en vogue. So I changed the number formatters attached to several NSTextFields to the "10.4 default behavior" and Decimal Style.

Everything works fine, except that the number formatter is no longer automatically localizing its decimal separator after switching from US (.) to German (,) locale. It seems I'm stuck with whatever was the active decimal separator at the time the number formatter was created in IB.

The "localize" checkbox in IB doesn't have an effect, as is expected, because setLocalizesFormat: is documented to only work for 10.0-style formatters (Leaving me wondering why the checkbox is even there).

My workaround right now is setting a custom NSNumberFormatter subclass in IB like this:

- (void) awakeFromNib
{
    self.decimalSeparator = [[NSLocale currentLocale] objectForKey:NSLocaleDecimalSeparator];
}

I can't believe that's the right way to do, as 10.0 style formatter didn't have this issue.

Any ideas what I'm missing?

Regards, Ilja

¿Fue útil?

Solución

I’ve confirmed you found a bug in Xcode’s interface builder—it’s freezing off the number grouping separator at the time of the XIB creation, which it shouldn’t do if the ‘Localize’ box is checked. Please file a RADAR.

The following code works correctly, and ensures all localized properties (decimal point, group separator, etc) will get set at run-time:

self.textFieldCell.formatter = [NSNumberFormatter new];
((NSNumberFormatter *)self.textFieldCell.formatter).numberStyle = NSNumberFormatterDecimalStyle;
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top