gpt4 book ai didi

xcode - 编码 NSMutableArray

转载 作者:行者123 更新时间:2023-12-04 22:49:31 24 4
gpt4 key购买 nike

我有一个包含各种 NSString 对象和变量的对象,我使用 NSCoding 将它们存档到磁盘上的文件中,然后再取消存档。到目前为止,一切都运行良好。

今天,我想向对象添加一个 NSMutableArray 并尝试使用以下代码进行编码:

[encoder encodeObject:myArray ForKey:@"myArray"];

然后使用以下方法对其进行解码:
[self setMyArray:[decoder decodeObjectForKey:@"myArray"]];

它似乎不起作用,虽然我在编码或解码本身中没有遇到任何错误,但如果我在从文件解码后尝试修改数组的值,我确实会收到错误。

我确定我在这里做的完全错误,但不完全确定是什么。我在想这可能与在解压缩期间没有正确分配有关。

有什么事情看起来很明显是问题的根源?

最佳答案

It doesn't appear to be working, and while I don't get any errors in the encoding or decoding itself, I do get an error if I try to modify the value of the array after decoding from the file.



解码文件会为您提供不可变的对象,无论它们在编码时是可变的还是不可变的。你没有做任何特别错误的事情——它像宣传的那样工作。

this answer对于类似问题的另一个示例,以及解决方案:
[self setMyArray:[[decoder decodeObjectForKey:@"myArray"] mutableCopy];

关于xcode - 编码 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10858895/

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