麻烦与NSDecimalNumber的decimalNumberByDividingBy:withBehavior: [英] Trouble with NSDecimalNumber's decimalNumberByDividingBy:withBehavior:

查看:779
本文介绍了麻烦与NSDecimalNumber的decimalNumberByDividingBy:withBehavior:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我总是似乎遇到麻烦NSDecimalNumber!今天,我收到此错误:

I always seem to run into trouble with NSDecimalNumber! Today, I get this error:


由于未捕获异常而终止应用程序'NSInvalidArgumentException' ** - [NSCFNumber decimalNumberByDividingBy:withBehavior:]:无法识别的选择器发送到实例0xd1fb10

是错误的根源:

- (void)setUpInstance {
    static NSDecimalNumberHandler* roundingBehavior = nil;
    if (roundingBehavior == nil) {
        roundingBehavior = [[NSDecimalNumberHandler alloc] initWithRoundingMode:NSRoundDown scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
    }   
    NSDecimalNumber *amount = viewController.aDecimalNumber;
    NSDecimalNumber *actualValue = viewController.anotherDecimalNumber;
    integralPortion = [amount decimalNumberByDividingBy:actualValue withBehavior:roundingBehavior];
    ...
}

文档将方法定义为:

-(NSDecimalNumber*)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber
                                withBehavior:(id<NSDecimalNumberBehaviors>)behavior

我不能解释(id< NSDecimalNumberBehaviors>)行为参数。这不是只是ANY对象,只要它符合NSDecimalNumbersBehaviors协议?

I must not be interpreting the "(id<NSDecimalNumberBehaviors>)behavior" argument correctly. Isn't that just ANY object as long as it conforms to the NSDecimalNumbersBehaviors protocol?

我做错了什么?

星期五快乐!

推荐答案


今天,我得到这个错误:终止应用程序由于未捕获异常'NSInvalidArgumentException',原因:'*** - [NSCFNumber decimalNumberByDividingBy:withBehavior:]:无法识别的选择器发送到实例0xd1fb10'

Today, I get this error: "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFNumber decimalNumberByDividingBy:withBehavior:]: unrecognized selector sent to instance 0xd1fb10'"

这意味着你发送消息到一个纯NSNumber(NSCFNumber幕后)。您只能将其发送到NSDecimalNumber。

That means you're sending that message to a plain NSNumber (NSCFNumber behind the scenes). You can only send it to an NSDecimalNumber.

请注意,如何声明变量是不相关的。你可以声明 amount NSString * amount ,你会得到完全相同的异常,包括NSCFNumber作为类名,因为它是一个异常,它发生在运行时。 (您当然也会收到关于NSString和NSDecimalNumber不可互换的编译时警告。)

Note that how you declare the variables is irrelevant. You could declare amount as NSString *amount and you would get the exact same exception, including NSCFNumber as the class name, because it is an exception, which happens at run-time. (You would, of course, also get the compile-time warning about NSString and NSDecimalNumber not being interchangeable.)

这篇关于麻烦与NSDecimalNumber的decimalNumberByDividingBy:withBehavior:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆