gpt4 book ai didi

objective-c - objective-c : updating values for key for an NSMutableDictionary?

转载 作者:行者123 更新时间:2023-12-03 17:42:13 26 4
gpt4 key购买 nike

我有一个 NSMutableDictionary,其中包含最初设置的键和值。后来我需要更新某些键的值,我想我只需要这样做[mutableDict setValue:val forKey:key],但这会引发异常:

[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object

我做错了什么?(我在可变字典的每个键内都有另一个可变字典,但我不认为这就是原因)

最佳答案

如果您想保持 100% 接受,我会复制粘贴我的评论

保罗.s

For some reason you are actually working with an NSDictionary not an NSMutableDictionary. Hard to say why without more code, maybe you are using a copy obtained by calling copy or you have loaded the dictionary from a plist/userDefaults.. Can we see some more code?

丹尼斯

@Paul.s Yes, I did copy an NSMutableDictionary. Would that be why? If it is, then what else should I use to copy?

保罗.s

Use mutableCopy instead of copy to get a mutable dictionary

丹尼斯

@Paul.s oh.. didn't know about that. I'm new to Objective-C and I tried to search for what I am doing wrong but I missed it :( Thank you!!

关于objective-c - objective-c : updating values for key for an NSMutableDictionary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10081078/

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