How to convert "SEL" and "id" to NSString?

21,081

Call NSStringFromSelector() passing your selector as its argument to get the selector string, and use [parent class] for the parent object's class:

NSString *errorMessage = [NSString stringWithFormat:@"%@ in class %@ doesn't exist!",
    NSStringFromSelector(selector), 
    [parent class]];
Share:
21,081
Manni
Author by

Manni

mobile development

Updated on May 18, 2020

Comments

  • Manni
    Manni almost 4 years
    id parent;
    SEL selector;
    
    // lot's of code...
    
    if ([parent respondsToSelector:selector]) {
    
    }
    else {
        // This doesn't work:
        NSString *errorMessage = [NSString stringWithFormat:@"%@ in class %@ doesn't exist!", selector, parent];
    }
    

    How do I convert "SEL" and "id" to a String?

  • Takagi
    Takagi almost 12 years
    The opposite way is: SEL aSelector = NSSelectorFromString(@"tapMeAction:");
  • owen gerig
    owen gerig over 11 years
    just wanted to point out that "@%s",_cmd use to work (and still does) but now post a warning. this seems to be the proper way
  • Benji XVI
    Benji XVI over 10 years
    Note - the format of the string returned by NSStringFromSelector is simply the selector, e.g. @"setTitle:". i.e. it has no @selector(~) wrapping etc.