奇怪的NSNumberFormatter崩溃

时间:2011-07-25 00:41:45

标签: objective-c nsnumberformatter invalid-argument

代码很简单:

static NSNumberFormatter *numberFormatter = nil;

if (numberFormatter == nil) {
    numberFormatter = [[NSDateFormatter alloc] init];
    [numberFormatter setNumberStyle:NSNumberFormatterDecimalStyle];
}

然而,它在最后一行崩溃了:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSDateFormatter setNumberStyle:]: unrecognized selector sent to instance 0x5c378d0'

怎么可能?

1 个答案:

答案 0 :(得分:4)

因为您创建了NSDateFormatter,而不是NSNumberFormatter。