在我的第一个函数中,我创建了一个 NSMutableDictionary 并将其保存在一个数组中。
NSMutableArray* tempPlayersArray = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] arrayForKey: @"kgpsTempArray"]];
NSMutableDictionary *tempPlayerDictArray = [[NSMutableDictionary alloc] init];
if (!(userDeviceName)) {
[tempPlayerDictArray setValue:userDeviceName forKey:@"DeviceName"];
}else{
[tempPlayerDictArray setValue:@"empty" forKey:@"DeviceName"];
}
[tempPlayersArray addObject:tempPlayerDictArray];
[defaults setObject:tempPlayersArray forKey:@"kgpsTempArray"];
[defaults synchronize];
在我的第二个函数中,我将其作为 NSCFDictionary - 这是不可变的。
NSMutableArray* tempPlayersArray = [NSMutableArray arrayWithArray: [[NSUserDefaults standardUserDefaults] arrayForKey: @"kgpsTempArray"]];
NSMutableDictionary *dictionaryForSearching = [[NSMutableDictionary alloc] init];
NSLog(@"%@",[dictionaryForSearching class]);
dictionaryForSearching = [tempPlayersArray objectAtIndex:index];
NSLog(@"%@",[[tempPlayersArray objectAtIndex:index] class]);
NSLog(@"%@",[dictionaryForSearching class]);
第一个日志显示“NSDictionaryM”。
第二个日志显示“NSCFDictionary”。
第三个也显示“NSCFDictionary”...
谁能解释一下为什么?以及如何解决?
最佳答案
NSUserDefaults 与不可变对象(immutable对象)一起工作,因此这就是当您返回字典时它已更改的原因。
你可以试试这个:
dictionaryForSearching = [[NSMutableDictionary alloc] initWithDictionary:[tempPlayersArray objectAtIndex:index]];
关于ios - MutableDictionary 变为不可变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15243945/