How to store non-objects in a dictionary?

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

+4
source share
3 answers

Wrap them in objects.

+1
source

You can convert the selector to NSString using NSStringFromSelector() , and you can return the other way using NSSelectorFromString() .

 SEL aSel = @selector(takeThis:andThat:); [myDict setObject:NSStringFromSelector(aSel) forKey:someKey]; SEL original = NSSelectorFromString([myDict objectForKey:someKey]); 
+15
source

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

You can also save the selector in the NSInvocation object and use it with a regular dictionary. If you need to store more than a selector (target, parameters, etc.), this is probably the best solution.

+2
source

All Articles