I have a list of const static double
s. I want to convert them to const static NSDecimalNumber*
s. Is this possible? If not, what's a good alternative?
Asked
Active
Viewed 126 times
0

Ky -
- 30,724
- 51
- 192
- 308
-
What happened when you tried it? – jscs Sep 08 '15 at 16:57
-
See (probably a duplicate) http://stackoverflow.com/questions/6143107/compiler-error-initializer-element-is-not-a-compile-time-constant – rmaddy Sep 08 '15 at 17:10
-
@JoshCaswell https://i.imgur.com/fvasgdT.png – Ky - Sep 08 '15 at 17:26
1 Answers
0
Unlike other Objective-C numbers (e.g. NSInteger), NSDecimalNumber
is a runtime-evaluated object, and thus cannot be evaluated at compile time. 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

Ky -
- 30,724
- 51
- 192
- 308