How to get double value from dictionary?

I am trying to get a double value from a dictionary. How to do this in objective-c?

+4
source share
2 answers

Dave is the answer to your previous question . To store a double value in an NSDictionary, you will need to set it to NSNumber.

To set a double value in a dictionary, you must use the following code:

[someDict setObject:[NSNumber numberWithDouble:yourDouble] forKey:@"yourDouble"]; 

and read it using the following:

 double isTrue = [[someDict objectForKey:@"yourDouble"] doubleValue]; 
+10
source

Brad Larson's answer is absolutely right. To tell you more about this, you must explicitly "wrap" the types of numbers without objects (for example, int , unsigned int , double , float , BOOL , etc.) In NSNumber when working with everything that an object expects.

On the other hand, some mechanisms in Objective-C, such as Key-Value Coding (KVC), will automatically make this packaging for you.

For example, if you have an int type @property called intProperty and you call the NSObject (NSKeyValueCoding) valueForKey: , for example [ someObject valueForKey:@"intProperty" ] , the result of the return is NSNumber * , NOT an int .

Honestly, I don’t need to switch between communicating with objects and non-object types (especially struct and enum s!) In Objective-C. I would prefer that everything be seen as an object, but maybe it's just me. :)

+1
source

All Articles