【问题标题】:Objective-C >> Capturing an Object Address in MemoryObjective-C >> 在内存中捕获对象地址
【发布时间】:2013-07-11 08:32:31
【问题描述】:

如何在代码中的某个点捕获对象的地址? 例如:

NSString * aString = @"bla bla";
//what is the current address of aString. i.e to which address in memory does it currently point to
aString = @"la la la";
//what is the current address of aString.

【问题讨论】:

    标签: objective-c memory-management memory-address


    【解决方案1】:

    例子:

    NSString *temp = @"123";
    
    uintptr_t ptrAddress = (uintptr_t) temp;
    
    NSLog(@"%@ %p %lu", temp, temp, ptrAddress);
    

    控制台:

    2013-07-11 11:51:20.796 asd[6474:907] 123 0x17985c 1546332

    它也可能对你有用 - NSPointerArray (iOS 6+)

    【讨论】:

      【解决方案2】:

      这很简单,只需这样做:

      NSLog(@"%p", aString);
      

      这是打印指针的格式规范。

      【讨论】:

      • 谢谢! “p”后面的类型是什么?假设我想把它保存在某个地方而不只是记录它?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2014-12-30
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多