iOS - 从包含地址的NSString获取指针 [英] iOS -- get pointer from NSString containing address

查看:157
本文介绍了iOS - 从包含地址的NSString获取指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个指向对象foo的地址(例如)0x809b5c0的指针,我可以通过调用

If I have a pointer to an object foo with address (say) 0x809b5c0, I can turn that into an NSString by calling

NSString* fooString = [NSString stringWithFormat: @"%p", foo].

这将给我NSString @0x809b5c0。

This will give me the NSString @"0x809b5c0".

扭转这个过程的最简单方法是什么?也就是说,从fooString开始,然后返回指向foo的指针。

What is the easiest way to reverse the process? That is, start with fooString, and get back my pointer to foo.

推荐答案

将其转换为整数,然后将其转换为它应该是什么类型...

Convert it to an integer, then cast it to whatever type it's supposed to be...

NSUInteger myInt = [ myStr integerValue ];
char * ptr       = ( char * )myInt;

那就是说,我真的不明白为什么你需要这个......

That said, I really don't understand why you will need this...

这篇关于iOS - 从包含地址的NSString获取指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆