Get string representation of id in Objective-C

Viewed 7783

How do you get the best string representation of an id object?

Is the following correct? Is there a simpler way?

id value;
NSString* valueString = nil;
if ([value isKindOfClass:[NSString class]]) {
    valueString = value;
} else if ([value respondsToSelector:@selector(stringValue)]) {
    valueString = [value stringValue];
} else if ([value respondsToSelector:@selector(description)]) {
    valueString = [value description];
}
3 Answers
Related