Is there a way to store a selector in an NSDictionary
, without storing it as an NSString
?
SEL
is just a pointer, which you could store in an NSValue
:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSValue valueWithPointer:@selector(foo)], @"foo",
nil];
To get the selector back, you can use:
SEL aSel = [[dict objectForKey:@"foo"] pointerValue];
An alternative to Georg's solution would be to convert the selector into an NSString before storing it the NSDictionary:
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
NSStringFromSelector(@selector(foo)), @"foo",
nil];
SEL selector = NSSelectorFromString([dict objectForKey:@"foo"]);
This technique, though uses more memory, gives you the ability to serialize the entire NSDictionary as a string via libraries like JSONKit.
An NSDictionary
is really just a CFDictionary
that retains and releases all keys and values. If you create a CFDictionary
directly, you can set it up to not retain and release values. You can typecast a CFDictionaryRef
to an NSDictionary *
and vice versa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With