0

使用我的应用程序时出现此错误。这是一个简单的计算器,当我按下一个数字时就会发生:

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;
4

2 回答 2

1

This has nothing to do with Core Data. You've subclassed NSNumber but haven't followed the rules laid out in the NSNumber documentation:

As with any class cluster, if you create a subclass of NSNumber, you have to override the primitive methods of its superclass, NSValue. Furthermore, there is a restricted set of return values that your implementation of the NSValue method objCType can return, in order to take advantage of the abstract implementations of the non-primitive methods. The valid return values are “c”, “C”, “s”, “S”, “i”, “I”, “l”, “L”, “q”, “Q”, “f”, and “d”.

If the only reason you've subclassed is to implement that getNumber: method, you should use an Objective-C Category instead.

In NSNumber+MyCategoryName.h:

@interface NSNumber (MyCategoryName)

- (NSString *)getNumberAsString:(float)n1;

@end

In NSNumber+MyCategoryName.m

@implementation NSNumber (MyCategoryName)

- (NSString *)getNumberAsString:(float)n1
{
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    [formatter setMaximumFractionDigits:2];
    [formatter setMinimumFractionDigits:0];
    NSString *nf = [formatter stringFromNumber:[NSNumber numberWithFloat: n1]];
    return nf;
}

@end

However, I don't think it's a good idea to put this method as a category on NSNumber. I could see it being a class method on NSNumberFormatter, perhaps. Finally, it's against the naming rules to call this method getNumber:; methods that start with get are usually reserved for methods that return multiple values through references passed in as arguments. Perhaps a better name would be -max2FractionDigits:.

于 2013-01-13T08:27:54.953 回答
1

我不认为这是核心数据问题。

尝试避免继承 NSNumber 并改用 category。

例如,您可以在这里查看

于 2013-01-13T06:05:39.467 回答