以下代码在尝试删除ObjectForKey时返回异常,并显示以下错误消息“mutate method sent to immutable object”
NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictDeviceIp"]; NSString *key = self.currentDeviceNameText.text; NSString *ipAddressTemp = [storedIpDictionary objectForKey:key]; [storedIpDictionary removeObjectForKey:key]; <----Crashes here storedIpDictionary[key] = ipAddressTemp;
不确定问题是什么,也许是因为从NSUserDefaults检索字典.
但是以下代码没有任何问题.
NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictDeviceIp"]; [storedIpDictionary removeAllObjects];
解决方法
NSUserDefaults返回不可变对象,即使你插入了可变对象.您必须在返回的值上调用-mutableCopy以获取可变集合.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。