Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing ints in a Dictionary

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!

Edit: 04/25/13

It's been a long time since I asked this question. For people stumbling on it in the future, there are easier ways to do this with the Apple LLVM Compiler 4.0, which has been default in Xcode for a bit. (ARC)

NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:@1337 forKey:@"1"];

That's it, use the @1337 Syntax to quickly create NSNumber objects. Works with Variables, so my above could become:

[dictionary setObject:@(varMoney) forKey:@"3"];

or

dictionary[@"mykey"] = @1337;

Simpler.

like image 701
Ethan Mick Avatar asked Nov 10 '09 01:11

Ethan Mick


People also ask

Can a dictionary store integers?

For example, the same dictionary can contain keys that are integers, floats, tuples, booleans, strings, and other types. But, due to this diversity of types of keys, dictionaries are considered to be unordered.

Can dictionary have integer keys?

Second, a dictionary key must be of a type that is immutable. For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable.

Can an object be a key in dictionary Python?

Properties of Dictionary Keys Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user-defined objects. However, same is not true for the keys.

Can you set a dictionary key?

The frozenset type is immutable and hashable — its contents cannot be altered after it is created; it can therefore be used as a dictionary key or as an element of another set.


2 Answers

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

like image 59
Carl Norum Avatar answered Oct 13 '22 23:10

Carl Norum


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.

like image 33
Jeff Avatar answered Oct 13 '22 23:10

Jeff