简体   繁体   English

如何在保留编码的情况下将货币字符串值设置为UITextField?

[英]How do I set a currency string value to a UITextField, preserving encoding?

I am developing an application in which I wish to handle different currency formats, depending on the current locale. 我正在开发一个应用程序,其中希望根据当前语言环境处理不同的货币格式。 Using a NSNumberFormatter I can correctly translate a number into string and back without problems. 使用NSNumberFormatter我可以正确地将数字转换为字符串,然后再无问题地转换回来。

But, if I put the string value into a UITextField and later get it back, I won't be able to convert the string back into a number and I will get a nil value instead. 但是,如果我将字符串值放入UITextField ,然后再将其取回,则无法将字符串转换回数字,而将获得nil值。

Here is a sample code to explain the problem: 这里是一个示例代码来说明问题:

NSNumberFormatter *nf = [Utils currencyFormatter];
NSNumber *n = [NSNumber numberWithInt:10000];
NSString *s = [nf stringFromNumber:n];
NSLog(@"String value = %@", s);
UITextField *t = [[UITextField alloc] init];
// I put the string into the text field ...
t.text = s;

// ... and later I get the value back
s = t.text;
NSLog(@"Text field text = %@", s);
n = [nf numberFromString:s];
NSLog(@"Number value = %d", [n intValue]);

where the currencyFormatter method is defined this way: 以这种方式定义currencyFormatter方法的位置:

+ (NSNumberFormatter *)currencyFormatter
{
    static NSNumberFormatter *currencyFormatter;
    if (!currencyFormatter) {
        currencyFormatter  = [[NSNumberFormatter alloc] init];
        [currencyFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
        [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
        [currencyFormatter setLocale:[NSLocale currentLocale]];
        if ([currencyFormatter generatesDecimalNumbers] || [[currencyFormatter roundingIncrement] floatValue] < 1) {
            [currencyFormatter setGeneratesDecimalNumbers:YES];
            [currencyFormatter setRoundingIncrement:[NSNumber numberWithFloat:0.01]];   
        }
    }

    return currencyFormatter;
}

(The inner if is used to force the formatter to always round to the smallest decimal digit, eg. even for CHF values). (内部的if用于强制格式化程序始终舍入到最小的十进制数字,例如,即使对于CHF值也是如此)。

What I get in the Console is this: 我在控制台中得到的是:

2012-03-29 00:35:38.490 myMutuo2[45396:fb03] String value = € 10.000,00
2012-03-29 00:35:38.494 myMutuo2[45396:fb03] Text field text = € 10.000,00
2012-03-29 00:35:38.497 myMutuo2[45396:fb03] Number value = 0

The strange part is that the spacing character between and 1 in the first line is represented in the console through a mid-air dot, while in the second line this dot disappears. 奇怪的是,第一行中1之间的空格字符是通过空中点在控制台中表示的,而第二行中该点消失了。 I believe this is an encoding-related problem. 我相信这是一个与编码有关的问题。

Can anyone help me solve this problem? 谁能帮我解决这个问题? Thank you! 谢谢!

Edit 编辑

I changed my test code to this: 我将测试代码更改为此:

NSNumberFormatter *nf = [Utils currencyFormatter];
NSNumber *n = [NSNumber numberWithInt:10000];
NSString *s = [nf stringFromNumber:n];
NSLog(@"String value = %@ (space code is %d)", s, [s characterAtIndex:1]);
UITextField *t = [[UITextField alloc] init];
t.text = s;
s = t.text;
NSLog(@"Text field text = %@ (space code is %d)", s, [s characterAtIndex:1]);
n = [nf numberFromString:s];
NSLog(@"Number value = %d", [n intValue]);

to discover this: 发现这一点:

2012-03-29 02:29:43.402 myMutuo2[45993:fb03] String value = € 10.000,00 (space code is 160)
2012-03-29 02:29:43.405 myMutuo2[45993:fb03] Text field text = € 10.000,00 (space code is 32)
2012-03-29 02:29:43.409 myMutuo2[45993:fb03] Number value = 0

The NSNumberFormatter writes down the space as a non-breaking space (ASCII char 160), and then the UITextField re-encodes that space as a simple space (ASCII char 32). NSNumberFormatter将空间记为不间断空格(ASCII字符160),然后UITextField将该空间重新编码为简单空间(ASCII字符32)。 Any known workaround for this behaviour? 此行为有任何已知的解决方法吗? Perhaps I could just make a replacement of the space with a non-breaking space but ... will it work for all the locales? 也许我可以用一个不间断的空格代替该空格,但是...它将适用于所有语言环境吗?

I was able to solve this problem only extending the UITextField through a custom class. 我只能通过自定义类扩展UITextField来解决此问题。 In this new class I put a new @property of type NSString in which I store the "computed" string value for the text field. 在这个新类中,我放置了一个新的@property ,类型为NSString在其中存储了文本字段的“计算”字符串值。 This string will never be modified and will preserve the original encoding of the content of the text field. 该字符串将永远不会被修改,并将保留文本字段内容的原始编码。

When you need to work again on the original untouched content of the text field you have to refer to this new property instead of referring to the text property. 当您需要再次处理文本字段的原始未修改内容时,必须引用此新属性,而不是引用text属性。

Using a separate string container is the only way to avoid these strange encoding changes. 使用单独的字符串容器是避免这些奇怪的编码更改的唯一方法。

A possible workaround: You could try to parse only the number values (and punctiation) via a regex pattern and create you currency value based on that number. 可能的解决方法:您可以尝试通过正则表达式模式仅解析数字值(和标点),然后根据该数字创建货币值。 If you do it in that way it is perhaps even more forgivable for the user, if he typed another currency symbol or other symbols that shouldnt be there... 如果您以这种方式进行操作,那么如果用户键入另一个货币符号或其他不应该存在的货币符号,对于用户来说甚至可以原谅...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM