简体   繁体   English

如何使用NSDecimalNumber作为const?

[英]How do I use an NSDecimalNumber as a const?

I have a list of const static double s. 我有一个const static double s列表。 I want to convert them to const static NSDecimalNumber* s. 我想将它们转换为const static NSDecimalNumber* s。 Is this possible? 这可能吗? If not, what's a good alternative? 如果没有,有什么好的选择?

Unlike other Objective-C numbers (eg NSInteger), NSDecimalNumber is a runtime-evaluated object, and thus cannot be evaluated at compile time. 与其他Objective-C数字(例如NSInteger)不同, NSDecimalNumber是运行时评估的对象,因此无法在编译时评估。 Due to this, you must treat it as the object it is. 因此,您必须将其视为对象。

So, replace your: 因此,请更换:

const static double YourNumber = 123.456;

@implementation YourClass
// ...
@end

with: 与:

NSDecimalNumber *YourNumber;

@implementation YourClass
+(void) initialize
{
    YourNumber = [NSDecimalNumber decimalNumberWithString:@"123.456"];
}

// ...
@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM