Reputation: 40030
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:
Upvotes: 2
Views: 1548
Reputation: 14633
None of these answers worked for me. So FWIW I do this:
po variableName.class
po type(of: variableName)
Upvotes: 1
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
Reputation: 6115
You can type this in the command window to print to classname:
po NSStringFromClass([object class])
Upvotes: 1