tom1990
tom1990

Reputation: 642

Simpler way to look up an NSDictionary key?

In a program I am working on I am using a C array of chars (it is potentially a very large array, so I do not want to use an NSArray). Each one is storing a value that corresponds to a particular status.

So that I can store some information about each status, I am using an NSDictionary. Each of the keys correspond to the possible status values. For each key, the value is a sub-dictionary that contains information I need corresponding to the status value.

Currently I am doing the following whenever I want to extract information from the NSDictionary:

for(i=0; i<arrayLen; i++) {

NSString *lookupKey1 = [[NSString alloc]initWithFormat:@"%i", array[i]];
int stateInfo1 = [[[statesDict objectForKey:lookupKey1] 
                                objectForKey:@"infoKey1"] intValue];
[lookupKey1 release];


NSString *lookupKey2 = [[NSString alloc]initWithFormat:@"%i", array[i]];
int stateInfo2 = [[[statesDict objectForKey:lookupKey2] 
                                objectForKey:@"infoKey2"] intValue];
[lookupKey2 release];


// Now do something with the values just obtained...

}

This works just fine, but what I don't like about having to do this is that I am having to allocate an NSString, just to look up a key. It feels like I am performing unnecessary operations. This is a particular concern to me since I am iterating through a large array and I don't want to be slowing it down by doing it in this way unless I absolutely have to.

When I try to simply pass the char in as I originally hoped I could to:

char stateInfo = [[[statesDict objectForKey:array[i]] 
                                objectForKey:@"infoKey"] charValue];

The compiler gives the warning "Passing argument 1 of objectForKey: makes pointer from integer without a cast", and the debugger throws an EXC_BAD_ACCESS exception.

I have also tried passing in a formatted string literal:

char stateInfo = [[[statesDict objectForKey:(@"%i", array[i])] 
                                objectForKey:@"infoKey"] charValue];

This throws up exactly the same error. Does anybody know if there is a more efficient way to do what I am trying to do, or is the way I described in the first snippet the 'correct' way to go about what I am trying to do?

Upvotes: 2

Views: 767

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726509

There are two things you can do to simplify your access:

  • Use NSNumber instead of NSString as keys of your NSDictionary
  • Use autoreleased keys to avoid calls of release

This would simplify your code a little:

int stateInfo2 = [[[statesDict objectForKey:[NSNumber numberWithInt:array[i]]] 
                            objectForKey:@"infoKey2"] intValue];

If the number of code is relatively small, there is a good chance that there would be no allocation of NSNumber in the current implementation: according to this link, [NSNumber numberWithInt:...] will return the same object for low values of the integer.

EDIT : Since property lists do not support keys of type other than NSString, you need to convert the dictionary obtained from plist to a dictionary with NSNumber keys like this:

NSDictionary *fromPlist = ... // This is your original dictionary
NSMutableDictionary *target = [NSMutableDictionary dictionary];
[fromPlist enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop){
    [target setObject:obj forKey:[key intValue]];
}];

Upvotes: 2

Chris Devereux
Chris Devereux

Reputation: 5473

NSDictionary* can only take objects as keys and values. When you pass it a char, it's casting it to a pointer.

@dasblinkenlight makes a good suggestion, although I guess you're slightly at the mercy of when the frameworks decide to give you a shared NSNumber instance, which could vary between OS releases.

Another option would be to rename your source file so that the extension is .mm and use a c++ std::map, which handles this sort of thing trivially.

#import <map>

@implementation MyClass {
  std::map<char, int> statesMap;

...

int stateInfo1 = statesMap[i];

That all said, I'd be worried that you're worrying unecessarily about this:

All these objects can't be good for performance

Even with an array with 10,000 elements, the memory overhead of using unique object keys is going to be about 80k. The equivalent of a couple of icons. Unless you're making hundreds of these, it's unlikely to cause memory issues. Performance-wise, go off data from the profiling tools, not assumptions. The bottlenecks are unlikely to be in the places you expect them to be in.

Upvotes: 2

Bot
Bot

Reputation: 11855

have you tried

char stateInfo = [[[statesDict objectForKey:[NSString stringWithFormat:@"%i", array[i]] 
                                objectForKey:@"infoKey"] charValue];

Upvotes: 1

Related Questions