开发者

NSKeyedArchiver with pointers

开发者 https://www.devze.com 2023-01-28 10:53 出处:网络
Hi if I want to archive a structure with pointers in it, where two different pointe开发者_StackOverflow中文版rs point the same place, then after the encoding-decoding do they point to the same place?

Hi if I want to archive a structure with pointers in it, where two different pointe开发者_StackOverflow中文版rs 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!


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]);
0

精彩评论

暂无评论...
验证码 换一张
取 消