从Objective-C中的实例十六进制获取类 [英] Get class from instance hex in Objective-C
问题描述
当在调试器中看到实例变量的地址时,如何通过输入给定的内存地址来获取类?
When seeing an instance variable's address in the debugger, how can one get the class by entering in the given memory address?
我知道相反(获取可以在调试器中使用 p someObjectInstance
或 NSLog(@%p,someObjectInstance);
从代码中。有没有类似的方法来反过来这样做?
I know that the opposite (getting the address from an instance) is possible with p someObjectInstance
in the debugger or NSLog(@"%p", someObjectInstance);
from within the code. Is there a similar way to do this the other way around?
推荐答案
你要求的是非常不安全的。访问一个未知的内存位置通常是一个坏主意,但因为你问:
What you are asking for is VERY unsafe. Accessing an unknown memory location is generally a bad idea, but since you asked:
编辑:如果在 gdb
内或 lldb
,您可以执行以下操作:
If inside gdb
or lldb
, you can do the following:
po [(id)(0xDEADBEEF) class]
但是,如果从代码运行,请使用以下内容;
If running from code, however, use the following;
NSString *input = @"0xFAFAFA";
unsigned address = UINT_MAX; // or UINT_MAX
[[NSScanner scannerWithString:input] scanHexInt:&address];
if (address == UINT_MAX)
{
// couldn't parse input...
NSLog(@"failed to parse input");
return 0;
}
void *asRawPointer = (void *) (intptr_t) address;
id value = (__bridge id) asRawPointer;
// now do something with 'value'
NSLog(@"%@", [value class]);
这篇关于从Objective-C中的实例十六进制获取类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!