Alex Rivera | Logout

How can I make an NSDictionary contain a selector as one of its values?

Asked 2009-07-23T21:49:09.637
10

This code is in a UITableViewController subclass viewDidLoad method. The UITableViewController subclass contains a test method.

It crashes without throwing an exception.

id dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"some text", @"text", @selector(test), @"selector", nil]
Edit
Report

1 Answer

14

pix0r's solution is good, but I usually prefer to use strings because they're more resilient to serialization and make the dictionary easier to read in debug output.

// Set selector
SEL inSelector = @selector(something:);
NSString *selectorAsString = NSStringFromSelector(inSelector);
id dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"some text", @"text", selectorAsString, @"selector", nil];

// Retrieve selector
SEL outSelector = NSSelectorFromString([dict objectForKey:@"selector"]);
answered 2009-07-23T23:23:03.973

Your Answer