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?
have you tried