Benches Uncategorized

7

Why NSCFString instead of NSString?

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Take a look at this snippet which I've got in Xcode:


NSString *myString = @"FOOBAR";
NSLog(@"Class name: %@", [myString className]);

It logs:


Class name: NSCFString

But why? Aren't I declaring a NSString object? And what is a NSCFString? I can't find anything about it.

I'm trying to see if certain objects are of the NSString type, but it isn't working because of the NSCFString:


if ([myObject isMemberOfClass:[NSString class]]) {
NSLog(@"Is Member of NSString: YES");
}

7 replies

Sign in or join Beachball Help to reply.

  1. 0

    Bang A. Lore

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    They are the same thing. One's a public interface and the other one's private. Stick to the public interface and you won't have any problems if Apple ever decides to change the private implementation.

  2. 0

    Bang A. Lore

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Because NSString and CFString represent different interfaces to the same underlying object whose name is kept hidden from view at all times except these.

  3. 0

    Heedless

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I get that NSString and NSCFString are not the same thing and that I can get my code to work with:

    if ([myObject isMemberOfClass:[NSString class]]) { 
    NSLog(@"Is Member of NSString: YES");
    }

    But I still find it odd that I say: make a NSString, but I end up with a NSCFString

  4. 0

    Bang A. Lore

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    They are the same thing. One's a public interface and the other one's private. Stick to the public interface and you won't have any problems if Apple ever decides to change the private implementation.

  5. 0

    Heedless

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Ok, I get it, thanks a lot

  6. 0

    MarkDouma®

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    They aren't the same thing. Read over
    Class Clusters for more info.

    Also, you should be careful when using isKindOfClass: (or isMemberOfClass:) with objects represented by a class cluster, as noted in
    NSObject Protocol reference.

    Depending on what you want to do after determining whether the object is an NSString or not, you could use isKindOfClass:.

    Hope this helps...

  7. 0

    Bang A. Lore

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    MarkDouma® wrote:

    They aren't the same thing.

    As far as he's concerned, they are. That's the magic of toll-free bridging.