How can I use an integer value as 'key' to set a float value in NSMutableDictionary ?
Asked
Active
Viewed 2,658 times
1 Answers
6
With NSNumber
literals you can do the following
[dict setObject:@5.562 forKey:@5];
//or
int key = 5;
float value = 5.562;
[dict setObject:@(value) forKey:@(key)];
And to retrieve:
float retrVal = [[dict objectForKey:@5] floatValue];
-
but how I could retrieve, if i doit like this? – BERGUIGA Mohamed Amine May 13 '13 at 18:15
-
The literals shouldn't be in parentheses. The parentheses are only used with variables. – rmaddy May 13 '13 at 18:15
-
@rmaddy `sed s/shouldn't/needn't/g` – May 13 '13 at 18:17
-
if i write like this, i get this error `incompatible integer to pointer conversion sending int to parameter of type id` – BERGUIGA Mohamed Amine May 13 '13 at 18:19
-
Make sure you include the `@` and that you are using LLVM 4.0 or higher ( >= Xcode 4.4 ). If not then you will need to manually create [`NSNumber`](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSNumber_Class/Reference/Reference.html)s for the keys and values. – Joe May 13 '13 at 18:24
-
`for(id key in Data_of_Transaction1) { NSDictionary* List_Bas = [Data_of_Transaction1 objectForKey:@(key)]; NSLog(@"%@",List_Bas); }` not work :( – BERGUIGA Mohamed Amine May 13 '13 at 18:28
-
@user2299789 What kinds of objects are in `Data_of_Transaction1`? You probably just want `NSDictionary* List_Bas = [Data_of_Transaction1 objectForKey:key];` or `NSDictionary* List_Bas = Data_of_Transaction1[key];` – rmaddy May 13 '13 at 18:31
-
1The `@` or `@()` syntax is used to wrap non-objects. In your loop, `key` is an object so you don't wrap the value. – rmaddy May 13 '13 at 18:38