xcode 如何获取类和运行时信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/771735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:32:18  来源:igfitidea点击:

How to get class and runtime information?

iphoneobjective-ccocoa-touchxcodedebugging

提问by Jake

For debugging purposes, I'd like to display as much class information as I can, and possibly runtime information (which thread does the class/function run in, etc) into the console.

出于调试目的,我想在控制台中显示尽可能多的类信息,以及可能的运行时信息(类/函数在哪个线程中运行等)。

Is there an easy way to do this with a function, variable or even (external) framework?

有没有一种简单的方法可以使用函数、变量甚至(外部)框架来做到这一点?

P.S: I'm using Cocoa Touch.

PS:我正在使用 Cocoa Touch。

回答by amattn

in a class, if you overload the -(NSString *)descriptionmethod, you can easily log class info with NSLog(@"%@", some_object);

在一个类中,如果您重载该-(NSString *)description方法,您可以轻松地记录类信息NSLog(@"%@", some_object);

here's a fictitious example:

这是一个虚构的例子:

-(NSString *)description
{
    return [NSString stringWithFormat:@"%@, %@, %d", 
                                      [super description], 
                                      class.object_ivar, 
                                      class.int_ivar];
}

You can use standard C macros to get things like name, file, line number etc... use the NSThread classes to get information about what thread the method is being called on.

您可以使用标准 C 宏来获取名称、文件、行号等内容……使用 NSThread 类获取有关调用该方法的线程的信息。

I posted this one to twitter. http://twitter.com/kailoa/status/1349928820Feel free to follow me if you are interested in more tidbits like this. I try to put them up regularly.

我把这个贴到推特上。 http://twitter.com/kailoa/status/1349928820如果您对更多这样的花絮感兴趣,请随时关注我。我试着定期把它们放好。

#define METHOD_LOG (NSLog(@"%@ %s\n%@", NSStringFromSelector(_cmd), __FILE__, self))