HelloMoon
HelloMoon

Reputation:

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. Example: There's a -fooBar method inside a SomeClass, and that -fooBar method runs some code. 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. Hard to explain. Any idea?

Upvotes: 29

Views: 18933

Answers (3)

Dr. chamran
Dr. chamran

Reputation: 540

Try:

[[self class] description]

Upvotes: 3

Jake
Jake

Reputation: 3973

NSLog(@"%@",[self className]);

Update: sorry, I didn't realize className didn't exist on the iPhone. As the above comment suggested; use ..

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

.. instead.

Upvotes: 10

jrbj
jrbj

Reputation: 731

On the Mac, you can use:

NSString *className = [self className];

or

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

On the iPhone, [self className] doesn't exist so you'll have to use:

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

Upvotes: 64

Related Questions