gpt4 book ai didi

ios - NSMutableDictionary 是不可变的

转载 作者:行者123 更新时间:2023-11-28 18:29:46 24 4
gpt4 key购买 nike

在 myClass.h 中,我有

@property (copy, atomic) NSMutableDictionary *thisParticularWordList;

在 myClass.m 中,我按如下方式填充 thisParticularWordList:

theSubview.thisParticularWordList = [NSMutableDictionary dictionaryWithDictionary: someDictionary];

在我看到的 xcode 的变量 View 中,确实填充了这个实例的属性。

在我的代码的其他地方,我尝试这样做:

[self.thisParticularWordList removeObjectForKey:self.theKey];

但不知何故,self.thisParticularWordList 变成了一个不可改变的 NSDictionary。

我做错了什么?

最佳答案

这是因为您的属性的 copy 属性。看看this answer .

一种解决方案是使属性strong,然后在分配它时,您可以执行self.yourProperty = [yourDictionary mutableCopy];

甚至 [NSMutableDictionary dictionaryWithDictionary: someDictionary]; 因为这也会创建一个新字典。


演示修复的完整代码示例:

MyClass.h

@property (strong, atomic) NSMutableDictionary *thisParticularWordList;

MyClass.m

theSubview.thisParticularWordList = [NSMutableDictionary dictionaryWithDictionary: someDictionary];

然后这将在分配后工作:

[self.thisParticularWordList removeObjectForKey:self.theKey];

关于ios - NSMutableDictionary 是不可变的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006484/

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