tags:

views:

46

answers:

3

I tried storing a selector(SEL) in a NSMutableDictionary and it caused a crash, probably because the dictionary tries to dereference it as an object pointer. What is the standard recipe for storing non-objects in a dictionary?

+1  A: 

Wrap them into objects.

spbfox
+6  A: 

You can convert selectors to NSString using NSStringFromSelector() and you can go back the other way with NSSelectorFromString().

SEL aSel = @selector(takeThis:andThat:);

[myDict setObject:NSStringFromSelector(aSel) forKey:someKey];

SEL original = NSSelectorFromString([myDict objectForKey:someKey]);
dreamlax
+2  A: 

Try using a NSMapTable with NSObjectMapKeyCallBacks and NSNonOwnedPointerMapValueCallBacks. This works like a NSMutableDictionary but allows any pointers as values, not just objects.

You also could store the selector in a NSInvocation object and use that with a regular dictionary. If you need to store more than the Selector (target, parameters and so on) this is probably the better solution.

Sven
I think `NSMapTable` is only available for Mac OS X, but the OP didn't specify his target platform.
dreamlax