Store selector as value in an NSDictionary

synic picture synic · May 12, 2010 · Viewed 10.8k times · Source

Is there a way to store a selector in an NSDictionary, without storing it as an NSString?

Answer

Georg Fritzsche picture Georg Fritzsche · May 12, 2010

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