I am trying to get a string name of a class from the class object itself.
// For instance
[NSArray className]; // @\"NSArray\"
I have found
Here's a different way to do it with slightly less typing:
NSString *name = [NSArray description];
NSString *name = NSStringFromClass ([NSArray class]);
You can even go back the other way:
Class arrayClass = NSClassFromString (name);
id anInstance = [[arrayClass alloc] init];
Consider this alternative:
const char *name = class_getName(cls);
It's much faster, since it doesn't have to alloc NSString object and convert ASCII to whatever NSString
representation is. That's how NSStringFromClass()
is implemented.