Objective-C类->像这样的字符串:[NSArray className]-> @"NSArray";

问题描述:

我正在尝试从类对象本身获取类的字符串名称.

I am trying to get a string name of a class from the class object itself.

// For instance
[NSArray className]; // @"NSArray"

我找到了object_getClassName(id obj),但这需要将实例传递给它,对于我而言,这是不必要的工作.

I have found object_getClassName(id obj) but that requires an instance be passed to it, and in my case that is needless work.

那么如何从类对象中获取字符串,而不是实例呢?

So how can I get a string from a class object, and not an instance?

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

您甚至可以返回另一种方式:

You can even go back the other way:

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