How to store non-objects in a dictionary?
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 t开发者_如何学Pythonhe standard recipe for storing non-objects in a dictionary?
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]);
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.
Wrap them into objects.
精彩评论