I was playing with the respondsToSelector method in Objective-C on MacOS-X 10.6.7 and Xcode 4.0.2, to identify if an object would respond to certain messages. According to t
This probably has to do with the implementation. NSString
is a class cluster, which means that NSString
is just a public interface and the actual implementing class is different (see what the class message gives you).
And at the same time NSString
is also toll-free bridged to CFString
, meaning that you can switch before those two types freely just by casting:
NSString *one = @"foo";
CFStringRef two = (CFStringRef)one; // valid cast
When you create a new string you really get a NSCFString
back, a thin wrapper around CFString
. And the point is that when you create a new mutable string, you also get an instance of NSCFString
.
Class one = [[NSString string] class]; // NSCFString
Class two = [[NSMutableString string] class]; // NSCFString
I guess this was convenient from the implementation point of view – both NSString
and NSMutableString
can be backed by a common class (= less code duplication) and this class makes sure you don’t violate the immutability:
// “Attempt to mutate immutable object with appendString:”
[[NSString string] appendString:@"foo"];
There’s a lot of guess work in this answer and I don’t really understand the stuff, let’s hope somebody knows better.