Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

easier way to populate a NSDictionary with integer keys?

1,239 views
Skip to first unread message

pablitoman

unread,
May 4, 2009, 1:16:44 PM5/4/09
to
Hi, I want to populate a NSDictionary with "integers" as keys, but it
looks like I need to convert them to NSNumbers first. Is there a less
tedious way to do this than what I show below?

NSDictionary *dict = [[NSDictionary alloc] init];
NSNumber *three = [[NSNumber alloc] initWithInt:3];
NSNumber *four = [[NSNumber alloc] initWithInt:4];
NSNumber *five = [[NSNumber alloc] initWithInt:5];
NSNumber *six = [[NSNumber alloc] initWithInt:6];
NSNumber *seven = [[NSNumber alloc] initWithInt:7];
NSNumber *eight = [[NSNumber alloc] initWithInt:8];

TKS!

Gregory Weston

unread,
May 4, 2009, 1:43:49 PM5/4/09
to
In article
<f5989ca5-ab71-4852...@o27g2000vbd.googlegroups.com>,
pablitoman <thebu...@gmail.com> wrote:

Yes. Especially given that when you alloc/init you're also going to have
to add the code to release the objects.

alloc/init the dictionary, of course, if you intend for it to be
persistent. But then add the entries as:

[dict setValue:foo forKey:[NSNumber numberWithInt:x]];

No (auto)release needed. That's assuming your dictionary is mutable. If
it's not mutable you'll have to do it all up front.

If your dictionary is *really* immutable - if it's going to be the same
every time you run your app - store it as a resource in your app package
and restore it using NSBundle methods to find it and
dictionaryWithContentsOfFile: to load.

If you really, really, really need the keys to be integers rather than
something that complies with the demands of NSDictionary, use
CFDictionary instead.

<http://developer.apple.com/documentation/CoreFOundation/Reference/CFDict
ionaryRef/Reference/reference.html>

--
I saw a truck today that had "AAA Batteries / Delivered and Installed" on the
side. My first thought was: That's a really weird business model. How many
inept people have urgent need of skinny little battery cells?

Pascal J. Bourguignon

unread,
May 4, 2009, 1:54:14 PM5/4/09
to

Yes, there is.

Your code doesn't populate the dictionary. You are just creating
an empty dictionary, and then creating NSNumber instances.

And you cannot modify a NSDictionary, only a NSMutableDictionary.


@interface NSDictionary
-(id)objectForInteger:(NSInteger)i;
@end

@interface NSMutableDictionary(IntegerKeys)
-(void)setObject:(id)object forInteger:(NSInteger)i;
@end

@interface NSDictionary(IntegerKeys)
-(id)objectForInteger:(NSInteger)i
{
return [self objectForKey:[NSNumber numberWithInteger:i]];
}
@end


@interface NSMutableDictionary(IntegerKeys)
-(void)setObject:(id)object forInteger:(NSInteger)i
{
[self setObject:object forKey:[NSNumber numberWithInteger:i]];
}
@end


and then you can fill it with a loop:

-(NSMutableDictionary*)dictionaryWithObjects:(NSArray*)objects forIntegersFrom:(NSInteger)from
{
NSMutableDictionary* dict=[NSMutableDictionnary dictionaryWithCapacity:[objects count]];
foreach(object in objects){
[dict setObject:object forInteger:from];
from++;
}
return(dict);
}


So you can write:

NSMutableDictionary* dict=[self dictionaryWithObjects:[NSArray arrayWithObjects:@"two",@"three",@"four",nil]
forIntegersFrom:2];


--
__Pascal Bourguignon__

Preston

unread,
May 4, 2009, 2:08:16 PM5/4/09
to

If you need a collection with objects accessed via integers, why not
use an array?

Gilles Kohl

unread,
May 4, 2009, 6:23:03 PM5/4/09
to
Preston,

I guess the (nicely consecutive) ints listed above are just examples,
if the values the OP has to deal with in real life are e.g. 32 bit
CRCs he wants to look up the corresponding file(s) for, an array with
2^32 NSString * (most of them nil) would take up a lot of space :-)

I'm quite new to Objective C / Cocoa though, dunno if the language or
library supports e.g. sparse arrays.

Regards,
Gilles.

0 new messages