Using an object as key for NSDictionary

Viewed 11967

I have a bunch of Lessons and the class works great. There is a view controller that works with these lessons. This controller needs to know the upload status of a lesson so we have a NSDictionary with the Lesson as key and an NSNumber having the percent of upload status.

This is a problem because after you insert a Lesson you want to later do a lookup on that same Lesson (perhaps in cellForRowAtIndexPath:) to get the progress. This does not work because keys are copied in NSDictionary.

Is it good form to save and fetch keys with something like this:

NSNumber *key = [NSNumber numberWithUnsignedInt:[obj hash]];
[dict setObject:@"... upload progress" forKey:key];

Or there a better approach?

4 Answers
UIView *view = [[UIView alloc] init];           
NSMapTable *dict = [NSMapTable weakToStrongObjectsMapTable];
[dict setObject:[NSNumber numberWithInt:1234] forKey:view];
NSNumber *x = [table objectForKey:view];
Related