#import <Foundation/Foundation.h>
typedef struct Node {
int offset;
} Node;
int main (int argc, const char * argv[])
{
@autoreleasepool {
NSMutableArray *array = [[NSMutableArray alloc] init];
Node node = {111111};
NSValue *value = [NSValue value:&node withObjCType:@encode(Node)];
[array addObject:value];
NSValue *structValue = [array objectAtIndex:0];
Node *n = (Node *)[structValue pointerValue];
printf("offset: %d", n->offset);
}
return 0;
}
The code crashes on this line: printf("offset: %d", n->offset); but why?
The right way to get c-value back is
getValue::pointerValuedoes not return a pointer to stored value, it reads a pointer value from memory where theNodeis stored (likeunion { Node n; void* pointerValue; }).