Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C class -> string like: [NSArray className] -> @"NSArray"

NSString *name = NSStringFromClass ([NSArray class]);

You can even go back the other way:

Class arrayClass = NSClassFromString (name);
id anInstance = [[arrayClass alloc] init];

Here's a different way to do it with slightly less typing:

NSString *name = [NSArray description];

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!