简体   繁体   中英

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:

在此处输入图片说明

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.

您可以在命令窗口中键入以下内容以打印到类名:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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