#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;
}
代码崩溃,在这条线:printf("offset: %d", n->offset);
但为什么呢?为什么这个简单的代码会导致EXC_BAD_ACCESS?
如果在printf行之前检查'n'的内容会发生什么? – 2012-03-06 14:10:16
我只是无法访问'n-> offset',但'printf(“%d”,n)'输出'111111'。但是如果我在'Node'结构中添加另一个字段,'printf(“%d”,n)'只打印第一个字段的值。 – neevek 2012-03-06 14:17:25