debug:获取对象的所有实例变量的列表(未知类型) [英] debug: Obtain a list of all instance variables of an object (unknown type)

查看:118
本文介绍了debug:获取对象的所有实例变量的列表(未知类型)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有使用lldb进行调试的方法(通过调试)获取一个未知对象的所有实例变量的列表,但是我承认我不太了解。



显然我看不到这个未知对象的标题。



我需要在调试时执行,但如果不可能,我可以在运行时使用另一种方法。



我发现这篇文章: 如何列出所有实例Objective-C中的类的变量,但是据我所知,我需要一个已知的类(我需要对象的头)



任何建议?

解决方案

利用您所链接的问题的接受答案的代码,您唯一需要做的是它成为一种方便的方法,以便您可以在调试期间随时调用它。在你的位置,我会写一个类扩展NSObject ,添加一个方法返回所有的ivars的NSDictionary ;这是一个例子:

   - (NSDictionary *)ivars 
{
NSMutableDictionary * ivarsDict = [NSMutableDictionary新];
unsigned int count;
Ivar * ivars = class_copyIvarList([self class],& count); (int i = 0; i< count; i ++)
{
Ivar ivar = ivars [i];
const char * name = ivar_getName(ivar);
const char * typeEncoding = ivar_getTypeEncoding(ivar);
[ivarsDict setObject:[NSString stringWithFormat:@%s,typeEncoding] forKey:[NSString stringWithFormat:@%s,name]];
}
免费(ivars);
return ivarsDict;
}

然后给出你不知道类型的对象,如果直接或间接继承自NSObject ,您只需打印从该方法返回的字典:

  lldb)po [someObject ivars] 

Credits:如何在Objective-C中列出类的所有实例变量?



PS:您需要导入 objc / runtime.h


Is there any method to obtain (via debug) a list of all instance variables of an unknown object in Objective-c?

I use lldb for debug, but I admit that I don't know it very well.

Obviously I can't look at the header of this unknown object.

I need to do it at debug time, but if it's not possible I can use an alternative at runtime.

I've found this post: How do I list all instance variables of a class in Objective-C? but, as I understand, I need to have a known Class (I need the headers of the object)

Any suggestion?

解决方案

Exploiting the code of the accepted answer of the question that you linked, the only thing that you need to do is to wrap it into a convenient method, so that you could call it at any time during debug. At your place I would write a category extending NSObject, adding a method that returns a NSDictionary with all the ivars; Here is an example:

- (NSDictionary*) ivars
{
    NSMutableDictionary* ivarsDict=[NSMutableDictionary new];
    unsigned int count;
    Ivar* ivars=class_copyIvarList([self class], &count);
    for(int i=0; i<count; i++)
    {
        Ivar ivar= ivars[i];
        const char* name = ivar_getName(ivar);
        const char* typeEncoding = ivar_getTypeEncoding(ivar);
        [ivarsDict setObject: [NSString stringWithFormat: @"%s",typeEncoding] forKey: [NSString stringWithFormat: @"%s",name]];
    }
    free(ivars);
    return ivarsDict;
}

Then given that object of which you don't know the type, if it directly or indirectly inherits from NSObject you just need to print the dictionary returned from this method:

(lldb) po [someObject ivars]

Credits: How do I list all instance variables of a class in Objective-C?

PS: You need to import objc/runtime.h .

这篇关于debug:获取对象的所有实例变量的列表(未知类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆