airportyh
airportyh

Reputation: 22678

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 the standard recipe for storing non-objects in a dictionary?

Upvotes: 4

Views: 590

Answers (3)

anon
anon

Reputation:

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.

Upvotes: 2

dreamlax
dreamlax

Reputation: 95385

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]);

Upvotes: 15

spbfox
spbfox

Reputation: 949

Wrap them into objects.

Upvotes: 1

Related Questions