0

I have an object that got parsed into an NSString and when I trace out the class name, it says NCDecimalNumber. Why? (I understand NSString is a cluster, but still don't understand why NSDecimalNumber would be a part of what's behind the cluster)

The following post asks a similar question but no one answers the why. Converting NSDecimalNumber to NSString

Monolo
  • 18,205
  • 17
  • 69
  • 103
Boon
  • 40,656
  • 60
  • 209
  • 315
  • 3
    Do you have any code that reproduces the situation? – Monolo Feb 25 '12 at 17:38
  • You can duplicate this when you parse a JSON with value in it and the object you get is similar to the post link I listed above. After that if you access the value (via objectForKey) and save it into a NSString (say of variable name "value", you will get NSDecimalNumber if you trace out [value class] – Boon Feb 25 '12 at 18:09

2 Answers2

2

Try to use this:

NSString *string = [[NSStrig alloc] initWithFormat:@"%@", [DecimalNumber stringValue]];

Hope that is going to help you.

Jonathan Grynspan
  • 43,286
  • 8
  • 74
  • 104
2

This sounds like the decoder decodes the number into an NSDecimalNumber (and strings into NSStrings).

Remember, Objective-C is C, so you can effectively assign anything to a pointer, it is up to you to ensure that the types correspond. This is why you can assign an object of type NSNumber to a pointer declared to be of type NSString*. As you can see, class clusters don't have anything to do with this.

So before assigning your object to a variable, you should check the class or, alternatively, just assign the object to a pointer of type id (which can hold any object).

If you need to work on the objects based on their type, you can do something like this:

id obj = //...

if ( [obj isKindOfClass: [NSString class]] ) {

}
else if ( [obj isKindOfClass: [NSNumber class]] ) {

}
else {

}
Monolo
  • 18,205
  • 17
  • 69
  • 103