Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS -- get pointer from NSString containing address

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].

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

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

like image 682
William Jockusch Avatar asked Apr 22 '11 14:04

William Jockusch


2 Answers

In its completeness...

To address-string

NSString *foo = @"foo";
NSString *address = [NSString stringWithFormat:@"%p", foo];

And back

NSString *fooAgain;
sscanf([address cStringUsingEncoding:NSUTF8StringEncoding], "%p", &fooAgain);
like image 189
hfossli Avatar answered Sep 20 '22 09:09

hfossli


This snippet worked for me:

NSString *pointerString = @"0x809b5c0";
NSObject *object;
sscanf([pointerString cStringUsingEncoding:NSUTF8StringEncoding], "%p", &object);
like image 38
PointerFun Avatar answered Sep 23 '22 09:09

PointerFun