简体   繁体   English

如何将方法类的名称作为NSString获取?

[英]How to get the name of a class of a method as NSString?

I need the name of the class that owns a method, as NSString . 我需要拥有一个方法的类的名称,如NSString Example: There's a -fooBar method inside a SomeClass , and that -fooBar method runs some code. 示例: SomeClass有一个-fooBar方法, -fooBar方法运行一些代码。 This code must print out what class "owns" it, I mean: What class that method belongs to. 这段代码必须打印出哪个类“拥有”它,我的意思是:该方法属于哪个类。 So I can't hard-type the class name in a NSString because I need that for debugging purposes, determining dynamically the name of the class. 所以我不能在NSString硬输入类名,因为我需要它来进行调试,动态确定类的名称。 Hard to explain. 难以解释。 Any idea? 任何想法?

On the Mac, you can use: 在Mac上,您可以使用:

NSString *className = [self className];

or 要么

NSString *className = NSStringFromClass([self class]);

On the iPhone, [self className] doesn't exist so you'll have to use: 在iPhone上, [self className]不存在,所以你必须使用:

NSString *className = NSStringFromClass([self class]);
NSLog(@"%@",[self className]);

Update: sorry, I didn't realize className didn't exist on the iPhone. 更新:抱歉,我没有意识到iPhone上不存在className。 As the above comment suggested; 如上述评论所示; use .. 采用 ..

NSLog(@"%@", NSStringFromClass([self class]));

.. instead. ..相反。

尝试:

[[self class] description]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM