How can I use an integer value as 'key' to set a float value in NSMutableDictionary ?
An integer key just means you assign a value to an integer (i.e. your name is just an integer).
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With