Rafał Sroka
Rafał Sroka

Reputation: 40030

How to log class name in Xcode debugger

I have an object that is of a type id. This object can be one of my custom classes instance. Is there a way I can log its class name?

I tried casting it to NSObject but I got an error.

error: C-style cast from 'id' to 'NSObject' is not allowed

Here full output of the console:

enter image description here

Upvotes: 2

Views: 1548

Answers (3)

Yusuf X
Yusuf X

Reputation: 14633

None of these answers worked for me. So FWIW I do this:

  • ObjC: po variableName.class
  • Swift: po type(of: variableName)

Upvotes: 1

Martin R
Martin R

Reputation: 539745

The class of an object is again an Objective-C object, so use po instead of p:

po [object class]

po is an abbreviation for expression -o -- and prints the description of the expression.


Your error is caused by the fact that id is a pointer and must be cast to NSObject *, not to NSObject. So this would work as well:

p [(NSObject *)object class]

Alternatively, cast the method's return type, as suggested by the lldb error message:

p (Class)[object class]

But po is the simplest solution for your problem.

Upvotes: 6

Thomas Keuleers
Thomas Keuleers

Reputation: 6115

You can type this in the command window to print to classname:

po NSStringFromClass([object class])

Upvotes: 1

Related Questions