views:

197

answers:

3

As I understand, in Objective-C you can only put Objects into dictionaries. So if I was to create a dictionary, it would have to have all objects. This means I need to put my ints in as NSNumber, right?

SOo...

NSNumber *testNum = [NSNumber numberWithInt:varMoney];


    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
    [dictionary setObject:@"OMG, Object 1!!!!" forKey:@"1"];
    [dictionary setObject:@"Number two!" forKey:@"2"];
    [dictionary setObject:testNum forKey:@"3"];


    NSNumber *retrieved = [dictionary objectForKey:@"3"];
    int newVarMoney = [retrieved intValue];

Where varMoney is an int that has been declared earlier. My question is, is there a better way to store "int" in a dictionary than putting it into a NSNumber?

Thanks!

+8  A: 

You are correct, NSNumber is the normal way to handle this situation. You can use NSValue or NSDecimalNumber, too.

Carl Norum
That's okay, I didn't think there was, so now I know. Thank you!
Wayfarer
A: 

Sorry but no :( Not as far as I know. Reason being that integers aren't objects, and NSDictionary only stores objects.

Jorge Israel Peña
That's okay, I didn't think there was, so now I know. Thank you!
Wayfarer
+4  A: 

If you are prepared to fall back to using Core Foundation (CFDictionaries) you can store anything you like. I've definitely made dictionaries into which I put arbitrary void* values.

You need to do a little more plumbing but not much, and its all documented.

Jeff