以下代码在尝试删除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];