我有一个 UITextField,我正在尝试使用 NSNumberFormatter 和 NSNumberFormatterDecimalStyle 将文本字段转换为 NSNumber 以初始化对象。
但是,当我这样做时,我无法从转换中获得任何明智的结果。出于本示例的目的,我已将 UITextField 替换为字符串,但仍然得到奇怪的结果。我确信我在做一些愚蠢的事情,但任何帮助将不胜感激。
//NSString * boardNumberText = self.txtBoardNumbs.text;
NSString * boardNumberText = @"42";
NSNumberFormatter * formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * boardNumber = [formatter numberFromString:boardNumberText];
if ([boardNumber isEqual:nil]) {
NSLog(@"Number was null");
}
NSLog(@"Number was not null");
[self.board setNumber:boardNumber];
NSLog(boardNumberText);
NSLog([NSString stringWithFormat:@"%d", boardNumber]);
NSLog([NSString stringWithFormat:@"%d", self.board.number]);
当我运行它时,我从日志文件中得到的输出是:
2012-07-15 16:54:26.564 CoreDataDev[16123:fb03] Number was not null
2012-07-15 16:54:26.564 CoreDataDev[16123:fb03] 42
2012-07-15 16:54:26.565 CoreDataDev[16123:fb03] 135821152
2012-07-15 16:54:26.565 CoreDataDev[16123:fb03] 135820272