NSNumberFormatter 在 nil 值上不返回 nilSymbol。我缺少什么?
iOS 4+;当我将 nil 值传递给 NSNumberFormatter 时,我想要设置的 nil 符号 (NSString *) 作为回报。它适用于“零”符号,但不适用于零符号。我尝试了许多不同的配置设置,例如行为、分组等。
fmtr = [[NSNumberFormatter alloc] init];
[fmtr setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtr setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[fmtr setUsesGroupingSeparator:NO];
[fmtr setNilSymbol:@"###"];
[fmtr setZeroSymbol:@"0000"];
NSLog(@"[fmtr nilSymbol]=>>%@<<", [fmtr nilSymbol]);
NSLog(@"[fmtr stringFromNumber:nil]=>>%@<< this should be '###", [fmtr stringFromNumber:nil] );
NSLog(@"[fmtr stringFromNumber:0]=>>%@<<", [fmtr stringFromNumber:[NSNumber numberWithInt:0]] );
NSLog(@"[fmtr stringFromNumber:null]=>>%@<<", [fmtr stringFromNumber:NULL] );
NSLog(@"[fmtr numberFromString:nil]=%@", [fmtr numberFromString:nil] );
NSLog(@"[fmtr numberFromString:@\"0\"]=%@", [fmtr numberFromString:@"0"] );
NSLog(@"[fmtr numberFromString:NULL]=%@", [fmtr numberFromString:NULL] );
这是测试的输出:
[fmtr nilSymbol]=>>###<<
[fmtr stringFromNumber:nil]=>>(null)<<这应该是'###
[fmtr stringFromNumber:0]=>>0000<<
[fmtr stringFromNumber:null]=>>(null)<<
[fmtr numberFromString:nil]=(null)
[fmtr numberFromString:@"0"]=0
[fmtr numberFromString:NULL]=(null)
iOS 4+; when I pass a nil value to my NSNumberFormatter I want the nil symbol that was set (NSString *) in return. It works for the 'zero' symbol, but not for the nil symbol. I've tried many different configuration settings like behavior, grouping, etc.
fmtr = [[NSNumberFormatter alloc] init];
[fmtr setNumberStyle:NSNumberFormatterDecimalStyle];
[fmtr setFormatterBehavior:NSNumberFormatterBehaviorDefault];
[fmtr setUsesGroupingSeparator:NO];
[fmtr setNilSymbol:@"###"];
[fmtr setZeroSymbol:@"0000"];
NSLog(@"[fmtr nilSymbol]=>>%@<<", [fmtr nilSymbol]);
NSLog(@"[fmtr stringFromNumber:nil]=>>%@<< this should be '###", [fmtr stringFromNumber:nil] );
NSLog(@"[fmtr stringFromNumber:0]=>>%@<<", [fmtr stringFromNumber:[NSNumber numberWithInt:0]] );
NSLog(@"[fmtr stringFromNumber:null]=>>%@<<", [fmtr stringFromNumber:NULL] );
NSLog(@"[fmtr numberFromString:nil]=%@", [fmtr numberFromString:nil] );
NSLog(@"[fmtr numberFromString:@\"0\"]=%@", [fmtr numberFromString:@"0"] );
NSLog(@"[fmtr numberFromString:NULL]=%@", [fmtr numberFromString:NULL] );
Here is the output of the test:
[fmtr nilSymbol]=>>###<<
[fmtr stringFromNumber:nil]=>>(null)<< this should be '###
[fmtr stringFromNumber:0]=>>0000<<
[fmtr stringFromNumber:null]=>>(null)<<
[fmtr numberFromString:nil]=(null)
[fmtr numberFromString:@"0"]=0
[fmtr numberFromString:NULL]=(null)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么它在使用 numberFromString 时不起作用,但我自己测试了这一点,如果您使用“stringForObjectValue”代替,它确实起作用。 (将所有“stringFromNumber”替换为“stringForObjectValue”)
找到答案
编辑:在此博客上 :http: //www.nsformatter.com/blog/2010/6/9/nsnumberformatter.html
它说:
这似乎是该方法的目的(不使用 nilSymbol)
I'm not sure exactly why it doesn't work when using numberFromString, but I tested this myself, and it does work if you use "stringForObjectValue" instead. (replace all "stringFromNumber" with "stringForObjectValue")
Edit: Found the answer
On this blog: http://www.nsformatter.com/blog/2010/6/9/nsnumberformatter.html
it says:
It seems to be the purpose of the method (not to use the nilSymbol)