从Objective-C中的实例十六进制获取类

Old*_*her 13 xcode objective-c ios

在调试器中查看实例变量的地址时,如何通过输入给定的内存地址来获取类?

我知道p someObjectInstance在调试器中或NSLog(@"%p", someObjectInstance);在代码中可以实现相反的(从实例获取地址).有没有类似的方法这样做反过来?

Ric*_*III 24

你要的是非常不安全.访问未知的内存位置通常是一个坏主意,但因为你问:

编辑:如果在里面gdblldb,您可以执行以下操作:

po [(id)(0xDEADBEEF) class]
Run Code Online (Sandbox Code Playgroud)

但是,如果从代码运行,请使用以下命令;

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]);
Run Code Online (Sandbox Code Playgroud)