I am working on some iOS code using Xcode 4.2.1, and I noticed that when I parse thru a plist file and place everything in an NSDictionary, all the <string> values end up in an NSString, but all the <real>, <integer>, <true/> values end up in an NSNumber.
Is there a way to extract all the keys and values, but place each value in it’s matching data type?
So for example:
Put <string>Hi There</string> value in an NSString
Put <real>1.23</real> value in a float
Put <integer>4</integer> value in an NSInteger
Put <true/> or <false/> or <boolean>1</boolean> value in a BOOL
I have read thru a lot of posts and cannot find anything to help me do this. I am not even sure how to detemine what the wrapper element (terminology might be wrong) around a value even is!
NSDictionary only has NSString or NSNumber so this won’t work.
Will an NSArray/NSMutableArray hold each value’s data type independently, or will I need to add a third item (value data type) to store along with the key and value?
Maybe I am trying to redo something that has already been done?
EDIT: Here are more details on what I am trying to do…
What I want to do is read in a set of keys/values from XML file and then make those keys available as variables within the game (with the key being the variable name and the variable being set to the value associated with the key). The variable will be retrieved thru a method like myVar = [getVar @"name_of_key"];
EDIT: Here is how I am currently putting the XML into NSDictionary…
NSDictionary *dictServer = nil;
CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)someXMLdata, kCFPropertyListImmutable, NULL);
if ([(id)plist isKindOfClass:[NSDictionary class]]) {
dictServer = [(NSDictionary *)plist autorelease];
}
NSArray & NSDictionary require you to insert objects (such as NSNumber), not primitives (such as int). Is there a reason you need the original data types?
UPDATE: NSNumber has class methods to wrap primitive values.
To retrieve the values, you can use