views:

243

answers:

3

Before putting int value into dictionary i have set the int value as [NSNumber numberWithInt:2], now when i try to retrieve back the dictionary content , i want it back in int format.. hw to do this??

here's my code;

NSMutabelDictionary *dict = [[NSMutableDictionary alloc]init];
int intValue = 300;

[dict setObject:[NSNumber numberWithInt:intValue] forKey:@"integer"];

retriving.........

int number = [dict ObjectForKey:@"integer"];

.. it throws an exception sayin casting is required.. when i do it this way..

int number = (int)[dict ObjectForKey:@"integer"];

stil it doesnt work...

how to solve this problem?/

please suggest..

A: 

Use the NSNumber method intValue

Here is Apple reference documentation

rano
+2  A: 

Have a look at the documentation. Use the intValue method:

int number = [[dict objectForKey:@"integer"] intValue];
Felix Kling
The method name is `objectForKey:`, not `ObjectForKey:` (there is a difference).
dreamlax
Felix Kling
A: 

You should stick to the NSInteger data types when possible. So you'd create the number like that:

NSInteger myValue = 1;
NSNumber *number = [NSNumber numberWithInteger: myValue];

Decoding works with the integerValue method then:

NSInteger value = [number integerValue];
Max Seelemann