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

查看:62
本文介绍了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];

这篇关于Objective-C类->像这样的字符串:[NSArray className]-> @"NSArray";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆