I declare the object variable as a NSString
But when I use the XCode to look into my object, I saw there are two type of String, it seems that the system automatically transfer to another:
What are the different between them? Are they interchangeable to one and others. Also, what is the condition two change to another?
Thanks.
One of benefits of transforming NSString to NSCFConstantString is next example:
For example - in method cellForRowAtIndexPath for tableView if you will write
than it would be the same address for every cell. But with NSLog(@"%p", &ident) it would be different address for every cell.
Reference to source (comments).
As far as I know,
NSCFConstantString
is an implementation ofNSString
that keeps the string data in code memory. Compiler creates instances of it when you use@"string"
constants. You can useNSCFConstantString
anywhere anNSString
could be used due to subclass/superclass relationship, but obviously not the other way around.It appears to be an optimization done by the compiler. I'm guessing that the string that is getting converted to an
NSCFConstantString
is equal to one of the constants that is cached for performance reasons. YourNSCFString
is just a toll-free bridged string that can be anNSString
or aCFString
. See this article for more information.They're both concrete subclasses of
NSString
.__NSCFString
is one created during runtime via Foundation or Core Foundation, while__NSCFConstantString
is either aCFSTR("...")
constant or an@"..."
constant, created at compile-time.Their interfaces are private. Treat them both as
NSString
and you should have no trouble.