“Is it better to style” to send a message and hope that the object is responding, or check if it responds to the selector and something like a backup, if it does not.
For instance:
- (NSString *)stringForObjectValue:(id)obj {
if ([obj respondsToSelector:@selector(intValue)]) {
NSString *roman = [self formatRomanNumber:[obj intValue] resultSoFar:@""];
return roman;
} else {
return [NSString stringWithFormat:@"can't format a %@", [obj class]];
}
}
against.
- (NSString *)stringForObjectValue:(id)obj {
NSString *roman = format_roman(@"", [obj intValue]);
return roman;
}
(example from a subclass of NSNumberFormatter ... but it can be from a subclass of NSObjectFormatter ...)
source
share