Reading in Number from Plist

Hello. I’m having a bit of an issue figuring out how to read in a number that’s stored in a Plist. I’m trying to port code over from Objective-C in cocos2d to the C*+ variant in cocos2d-x. It’s pretty straightforward in Objective-C but I can’t seem to figure out how to do it right with C*+. I have no problems reading in Strings, but when it comes to Numbers, I’m kinda stuck…

I think they come in as strings.
you can get the floatValue from them somehow.

I think they come in as strings.
you can get the floatValue from them somehow.

daniel fountain wrote:

I think they come in as strings.
you can get the floatValue from them somehow.

Or the C++ *atoi( const char * )* function.

Thanks for the replies.

Yep, I know about the atoi function and have been using it, although I was thinking there would be a NSNumber or NSValue equivalent in cocos2d-x to work with Number value type in the plist.

it doesnt,
if you delve into the dictionary code it appears to interprete numbers as strings, at least in ios.
search through functions / objects like this to see what they do.
all the best
Dan

Alrighty, thanks. _

You can read a number from a PList like this:

//create a CCDictionary with the contents of the PList
CCDictionary* dictionary = CCDictionary::createWithContentsOfFile("mypList.plist");

//Get a value by key and convert to integer
int value = dictionary->valueForKey("MY_KEY")->integerValue();

Example of other conversion types

//float
int value = dictionary->valueForKey("MY_KEY")->floatValue();

//bool
bool value = dictionary->valueForKey("MY_KEY")->boolValue();

//char*
char* value = dictionary->valueForKey("MY_KEY")->getCString();

//e.t.c...

Can do pretty much same thing with a CCArray stored into a PList.