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!
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?
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__
If you need a collection with objects accessed via integers, why not
use an array?
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.