How do I access floats from a dictionary of floats?
I have a property list (Data.plist
) that contains an array of two dictionaries. Each dictionary is filled 开发者_运维百科with key names (Factor 1
, Factor 2
, etc) and floats (0.87
, 1.15
, etc.). I am trying to access the numbers stored in the dictionary. First I load the dictionary using:
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Data.plist"];
NSArray *plistData = [[NSArray arrayWithContentsOfFile:finalPath] retain];
NSDictionary *dictionaryOne = [plistData objectAtIndex:0];
Actually accessing the numbers stored is where I'm having a problem:
Float32 conversionFactor = [scyToLCMMen objectForKey:"Factor 50"];
I'm getting the error: "incompatible types in initialization
". What am I doing wrong? Is it not a Float32
?
Objective-c containers can only hold obj-c types, so what you get is not a float for sure. What you probably have is NSNumber object and you need to "extract" plain float value from it:
Float32 conversionFactor = [[scyToLCMMen objectForKey:"Factor 50"] floatValue];
NSNumber *conversionFactor = [dictionaryOne valueForKey:@"Factor 50"];
float factor = [conversionFactor floatValue];
That's what I'd do
The value is likely an instance of NSNumber.
Float32 conversionFactor = [[scyToLCMMen objectForKey:"Factor 50"] floatValue];
精彩评论