Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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

po NSStringFromClass([object class])

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

  • ObjC: po variableName.class
  • Swift: po type(of: variableName)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top