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?
+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
2010-08-26 04:21:16
+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
2010-08-26 06:39:28
I think `NSMapTable` is only available for Mac OS X, but the OP didn't specify his target platform.
dreamlax
2010-08-26 07:34:03