NSString instance reports its class as NSCFString
Asked Answered
S

2

20

My objective here is really simple -- I'm trying to set an NSString to some test data, then return the class, which should be NSString. Here's my code:

NSString* stringer = [NSString stringWithFormat: @"Test"];
NSLog(@"%@", [stringer class]);

The log says that the class is NSCFString, not NSString. What's going on here?

Standee answered 26/12, 2008 at 15:12 Comment(0)
V
30

NSString is really a container class for different types of string objects. Generally an NSString constructor does return an object that is actually of type NSCFString, which is a thin wrapper around the Core Foundation CFString struct.

Varden answered 26/12, 2008 at 15:21 Comment(0)
E
25

NSString is a class cluster, along with other Foundation types such as NSNumber and NSArray:

Class clusters are a design pattern that the Foundation framework makes extensive use of. Class clusters group a number of private, concrete subclasses under a public, abstract superclass. The grouping of classes in this way simplifies the publicly visible architecture of an object-oriented framework without reducing its functional richness. Class clusters are based on the Abstract Factory design pattern discussed in “Cocoa Design Patterns.”

Etrem answered 26/12, 2008 at 17:12 Comment(1)
Why did Apple not follow this approach for NSOrderedSet? A NSOrderedSet behaves exactly like a NSArray at the interface level, yet they made a independent class for it.Eleaseeleatic

© 2022 - 2024 — McMap. All rights reserved.