IOS - get a pointer from an NSString containing an address

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

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

This will give me NSString @ "0x809b5c0".

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

+5
source share
5 answers

Convert it to an integer, and then apply it to any type that should be ...

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

However, I really don't understand why you need this ...

+2
source

In its entirety ...

To address bar

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

And back

NSString *fooAgain;
sscanf([address cStringUsingEncoding:NSUTF8StringEncoding], "%p", &fooAgain);
+9
source

:

NSString *pointerString = @"0x809b5c0";
NSObject *object;
sscanf([pointerString cStringUsingEncoding:NSUTF8StringEncoding], "%p", &object);
+3

NSInteger, :

MyObject *object=[[MyObject alloc]init];
//...
NSInteger adress=(NSInteger)object;

:

MyObject *otherObject=(MyObject*)adress;

: object == otherObject

+1
source

Use NSMapTable instead:

MyClass* p = [[MyClass alloc]init];
NSMapTable* map = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory valueOptions:NSMapTableStrongMemory];
[map setObject:whateverValue forKey:p];
// ...
for(MyClass* key in map) {
// ...
}
0
source

All Articles