Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use an integer value as 'key' to set value in NSMutableDictionary?

How can I use an integer value as 'key' to set a float value in NSMutableDictionary ?

like image 713
Ratan Avatar asked Jan 27 '11 10:01

Ratan


People also ask

What is an integer key?

An integer key just means you assign a value to an integer (i.e. your name is just an integer).

How do you add value in Nsmutabledictionary?

You can simply say: myDictionary[myWord] = nextValue; Similarly, to get a value, you can use myDictionary[key] to get the value (or nil). The simplified form mentioned above, usually solves problems OCLint (metric static code analysis).


1 Answers

As NSDictionarys are only designed to deal with objects, a simple way to do this is to wrap the integer and float in a NSNumber object. For example:

NSMutableDictionary *testDictionary = [[NSMutableDictionary alloc] init]; [testDictionary setObject:[NSNumber numberWithFloat:1.23f]                    forKey:[NSNumber numberWithInt:1]]; NSLog(@"Test dictionary: %@", testDictionary);  [testDictionary release]; 

To extract the relevant value, simply use the appropriate intValue, floatValue, etc. method from the NSNumber class.

like image 97
John Parker Avatar answered Sep 27 '22 16:09

John Parker