一个计算器。每次我点击一个数字按钮时,标签都会附加到一个“displaystring”nsmutable 字符串。我注意到,在为相同的数字输入 11 位数字后,floatValue 或 intValue 函数给了我同样的错误。
self.lbldisplay.text 正确显示 displaystring 的内容。但是 intValue 或 floatValue 甚至 NSCanner 实用程序在输入 10 位数字后返回相同的错误
这是代码和日志:
// original
[self.displayString appendString: [NSString stringWithFormat: @"%i", [sender tag]]];
else { // new entry
//origin test
[self.displayString setString:@""];
[self.displayString appendString: [NSString stringWithFormat: @"%i", [sender tag]]];
bIsTypingANumber = TRUE;
}
// fCurrentNumber = [self.displayString floatValue];
// [self display:fCurrentNumber];
// self.lblDisplay.text = displayString;
NSString *numberString;
NSString *str = [NSString stringWithString:displayString];
NSScanner *theScanner = [NSScanner scannerWithString:str];
[theScanner scanUpToCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:nil];
[theScanner scanCharactersFromSet:[NSCharacterSet decimalDigitCharacterSet] intoString:&numberString];
NSLog(@"Attempts: %i", [numberString integerValue]);
self.lblDisplay.text = str;
输出:
2012-08-26 17:23:34.264 Pilots Fuel[4989:f803] Attempts: 1
2012-08-26 17:23:34.400 Pilots Fuel[4989:f803] Attempts: 11
2012-08-26 17:23:34.552 Pilots Fuel[4989:f803] Attempts: 111
2012-08-26 17:23:34.800 Pilots Fuel[4989:f803] Attempts: 1111
2012-08-26 17:23:34.936 Pilots Fuel[4989:f803] Attempts: 11111
2012-08-26 17:23:35.072 Pilots Fuel[4989:f803] Attempts: 111111
2012-08-26 17:23:35.216 Pilots Fuel[4989:f803] Attempts: 1111111
2012-08-26 17:23:35.344 Pilots Fuel[4989:f803] Attempts: 11111111
2012-08-26 17:23:35.488 Pilots Fuel[4989:f803] Attempts: 111111111
2012-08-26 17:23:35.632 Pilots Fuel[4989:f803] Attempts: 1111111111
2012-08-26 17:23:35.776 Pilots Fuel[4989:f803] Attempts: 2147483647
2012-08-26 17:23:36.056 Pilots Fuel[4989:f803] Attempts: 2147483647