使用我的应用程序时出现此错误。这是一个简单的计算器,当我按下一个数字时就会发生:
2013-01-12 21:13:32.175 Basic Calc[1683:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***
-objCType only defined for abstract class. Define -[FormatNumber objCType]!'
*** First throw call stack: (0x1c8f012 0x10cce7e 0x1c8edeb 0xb96f35 0xba6b3f 0xb1a049 0xac8fc5 0x1c09ac6 0x1c4f13c 0xac8f60 0xac8eab 0x2b4b 0x10e0705 0x17920 0x178b8 0xd8671 0xd8bcf 0xd7872 0x4716d 0x47552 0x253aa 0x16cf8 0x1beadf9 0x1beaad0 0x1c04bf5 0x1c04962 0x1c35bb6 0x1c34f44 0x1c34e1b 0x1be97e3 0x1be9668 0x1465c 0x27cd 0x26f5) libc++abi.dylib: terminate called throwing an exception
这是我的代码的一部分:
主视图控制器.m
currentNumber = currentNumber *10 + (float)[sender tag];
FormatNumber *convertedNumber;
convertedNumber = [[FormatNumber alloc] init];
[convertedNumber getNumber:currentNumber];
calculatorScreen.text = [NSString stringWithFormat:@"%@",convertedNumber];
格式编号.m
@interface FormatNumber: NSNumber
{
NSString *nf;
}
-(NSString *) getNumber:(float)n1;
@end
格式编号.m
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:0];
nf = [formatter stringFromNumber:[NSNumber numberWithFloat: n1]];
return nf;
此外,当我收到错误时,代码指向 main.m 中的这一行:
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
有任何想法吗?
新代码:
主视图控制器.h
currentNumber = currentNumber *10 + (float)[sender tag];
NSNumber *convertedNumber = [[NSNumber alloc] init];
NSString *finalValue = [convertedNumber customFormatNumber:currentNumber];
calculatorScreen.text = [NSString stringWithFormat:@"%@",finalValue];
NSNumber+FormatNumber.h
@interface NSNumber (FormatNumber)
-(NSString *) customFormatNumber:(float)n1;
@end
NSNumber+FormatNumber.m
@implementation NSNumber(FormatNumber)
-(NSString *) customFormatNumber:(float)n1
{
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setMaximumFractionDigits:2];
[formatter setMinimumFractionDigits:0];
NSString *nf = [formatter stringFromNumber:[NSNumber numberWithFloat:n1]];
return nf;