如何在iOS上获取符号地址?

时间:2014-03-25 06:55:47

标签: ios objective-c

我正在构建一个用于从Python访问Objective-C的库。我一直坚持在iOS上获取变量的地址。

我想说我想获得CBCentralManagerScanOptionAllowDuplicatesKey的指针地址:

NSString *key = CBCentralManagerScanOptionAllowDuplicatesKey;
NSLog(@"Address is: %p\n", key);

NSString *key2 = dlsym(RTLD_SELF, "CBCentralManagerScanOptionAllowDuplicatesKey");
NSLog(@"Address2 is: %p\n", key2);

我得到了:

Address is: 0x3a827fcc
Address2 is: 0x3a825514

为什么我会得到不同的价值观?我尝试使用RTLD_NEXT查找,仍然获得相同的值。 Objective-c变量是否会以某种方式被破坏?

1 个答案:

答案 0 :(得分:4)

dlsym()为您提供CBCentralManagerScanOptionAllowDuplicatesKey地址 变量,而不是 contents ,它是指向Objective-C字符串的指针。

NSLog(@"Address  is: %p\n", & CBCentralManagerScanOptionAllowDuplicatesKey);
NSLog(@"Key      is: %p\n", CBCentralManagerScanOptionAllowDuplicatesKey);

void *addr2 = dlsym(RTLD_SELF, "CBCentralManagerScanOptionAllowDuplicatesKey");
NSLog(@"Address2 is: %p\n", addr2);
// Dereference pointer to get its contents:
NSString *key2 = *(NSString * __unsafe_unretained *)addr2;
NSLog(@"Key2     is: %p\n", key2);

输出:

Address  is: 0x7fff78950388
Key      is: 0x7fff7894ec08
Address2 is: 0x7fff78950388
Key2     is: 0x7fff7894ec08