[英]Is there a way to get the memory address of any object as an NSString?
I need to store the memory address of an Object as an NSString. 我需要将Object的内存地址存储为NSString。 Is there a special format specifier for that? 是否有特殊的格式说明符?
%p
should work for any pointer, including pointers to objects. %p
应该适用于任何指针,包括指向对象的指针。 So: 所以:
NSString *addr = [NSString stringWithFormat:@"%p", obj];
That will be a string beginning with 0x
and in hexadecimal. 这将是一个以0x
并以十六进制开头的字符串。
Yes, you can use the %p
formatter to get the address of an object as a string. 是的,您可以使用%p
格式化程序将对象的地址作为字符串。 Something like this would work: 像这样的东西会起作用:
NSString *pstr = [NSString stringWithFormat:@"%p", myPtr];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.