gpt4 book ai didi

ios - 自引用 NSMutableDictionary

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:30:03 26 4
gpt4 key购买 nike

假设我有这个:

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[@1] = @2;
dict[@3] = dict;

我通过调用归档 dict:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dict];

然后我稍后取消存档:

NSMutableDictionary *dict2 = [NSKeyedUnarchiver unarchiveObjectWithData:data]

问题是 dict2[@3] 不是 dict2,而是一个未初始化的 NSDictionary,我无法恢复我输入的内容。有谁知道我将如何解决这个问题?提前致谢!

最佳答案

Apple 邮件列表上的一位 Apple 工程师对此进行了解释:http://lists.apple.com/archives/cocoa-dev/2007/May/msg00747.html ,在回答关于归档 NSMutableArray 的类似问题时将自身包含为一个元素。

总结:

This (very probably -- I haven't looked into it) is not a problem with the recursion per-se, but rather with objects which replace themselves during unarchiving by returning a new object from initWithCoder:.
...
So the summary answer is: you can't reliably have recursive references. In practice, though, these do occur (think of the NSView subview/superview relationship) and things squeak by. Sometimes they don't and it's impossible to know beforehand if something will work or not.

Chris Kane
Cocoa Frameworks, Apple

关于ios - 自引用 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com