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”转换为字符串?
答案 0 :(得分:83)
调用NSStringFromSelector()
传递选择器作为其参数以获取选择器字符串,并使用[parent class]
作为parent
对象的类:
NSString *errorMessage = [NSString stringWithFormat:@"%@ in class %@ doesn't exist!",
NSStringFromSelector(selector),
[parent class]];