2

I have a case where I need to have NSMutableDictionary with NSManagedObject as the key.

Based on this post, I can set NSManagedObject as key in dictionary by: [NSValue valueWithNonretainedObject:]

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:product forKey:[NSValue valueWithNonretainedObject:category]];

How can I get back the value of dict? I've tried using NSValue again but it crash with no description.

Community
  • 1
  • 1
Rendy
  • 5,572
  • 15
  • 52
  • 95

1 Answers1

2

Try using [theValue nonretainedObjectValue]

But if you want to access the keys frequently, a dictionary might not be the right data structure for you. Especially if you want some kind of inverse relationship with objects and keys (if that is what you mean with get back the value of dict).

Hjalmar
  • 989
  • 8
  • 16
  • no luck..it still crash :( yeah i know, i think i need to think another way around..thanks! – Rendy Dec 21 '12 at 01:23
  • @Rendy When I have done this I've used `NSManagedObjectID` for key. Then use `existingObjectWithID`to get the actual object. I know this is not what you are asking for, but in my opinion it is an easier solution. I hope it helps. – Hjalmar Dec 21 '12 at 11:40