user408141
user408141

Reputation:

NSKeyedArchiver with pointers

Hi if I want to archive a structure with pointers in it, where two different pointers point the same place, then after the encoding-decoding do they point to the same place?

Sorry for my bad english, and thx ahead for your answers and free time!

Upvotes: 1

Views: 196

Answers (1)

Dan Rosenstark
Dan Rosenstark

Reputation: 69757

Yes. If you have two keys that point to the same object, NSKeyArchiver will restore them as keys pointing to the same object.

NSString *one = @"1";
NSString *two = @"2";
NSMutableDictionary *dict = [[[NSMutableDictionary alloc] initWithObjectsAndKeys:one, @"one", two, @"two", one, @"three", nil] autorelease];
NSLog(@"one=%d, two=%d, three=%d", [[dict objectForKey:@"one"] hash], [[dict objectForKey:@"two"] hash], [[dict objectForKey:@"three"] hash]);
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict];

dict = nil; // no tricks!

dict = [NSKeyedUnarchiver unarchiveObjectWithData:data];

NSLog(@"one=%d, two=%d, three=%d", [[dict objectForKey:@"one"] hash], [[dict objectForKey:@"two"] hash], [[dict objectForKey:@"three"] hash]);

Upvotes: 3

Related Questions