If you store NSNumber* as a key to NSMutableDictionary, does it do the lookup later based on the key's address or stored value? It seems that if I store NSNumber in two dictionaries with the value from one serving as the key to the other, sometimes, the key isn't found in the 2nd dictionary even though I'm certain it's in there.
What I'm trying to do is the following:
// fetch the items from Coredata and create two different lookups
NSArray *existingActions = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
for (SomeObject *someObject in existingActions)
{
NSNumber *actionId = [someObject actionId];
[actionMap setObject:someObject forKey: actionId];
NSNumber *positionId = [someObject positionId];
[positionMap setObject: actionId forKey: positionId];
}
// later on, I'm trying to print out the values from both lookups now
NSEnumerator *enumerator = [positionMap keyEnumerator];
NSNumber *positionId;
while ((positionId = (NSNumber *)[enumerator nextObject]))
{
NSNumber *actionId = (NSNumber *)[positionMap objectForKey:positionId];
SomeObject * myObj = (SomeObject *) [actionMap objectForKey:actionId];
NSLog(@"myObj: %@", myObj];
}
// This way yields the same result as above:
NSNumber *positionId = [NSNumber numberWithInt: 7]; // filled out by client
NSNumber *actionId = (NSNumber *)[positionMap objectForKey:positionId];
SomeObject *myObj = (SomeObject *)[actionMap objectForKey: actionId];
Often, myObj is null because objectForKey can't seem to find the object associated with actionId in the actionMap dictionary.
As an additional note, the SomeObject class derives from 'NSManagedObject'. The documentation for setObject:forKey: notes for the parameters passed in:
aKey: The key for value. The key is copied (using copyWithZone:; keys must conform to the NSCopying protocol). The key must not be nil.
Is it due to the fact that copyWithZone may be different with NSManagedObject classes?
What am I doing wrong here?