如何将“SEL”和“id”转换为NSString?

时间:2010-12-20 13:29:36

标签: ios objective-c nsstring selector

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

如何将“SEL”和“id”转换为字符串?

1 个答案:

答案 0 :(得分:83)

调用NSStringFromSelector()传递选择器作为其参数以获取选择器字符串,并使用[parent class]作为parent对象的类:

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