我无法保存 NSMutableDictionary。我正在尝试将其保存到 NSUserDefaults,然后加载并更改为蓝色,然后再次保存。
所以我最初是这样写的:
NSMutableDictionary *d = [NSMutableDictionary dictionaryWithObjectsAndKeys:
firstNameLbl, @"firstName",
lastNameLbl, @"lastName",
nil];
[[NSUserDefaults standardUserDefaults] setObject:d forKey:@"youDict"];
[[NSUserDefaults standardUserDefaults] synchronize];
然后我加载它并尝试更改如下值:
NSMutableDictionary *d = [[NSUserDefaults standardUserDefaults] objectForKey:@"youDict"];
[d setObject:textField.text forKey:key];
[[NSUserDefaults standardUserDefaults] setObject:d forKey:@"youDict"];
[[NSUserDefaults standardUserDefaults] synchronize];
但是,这行 ,[d setObject:textField.text forKey:key];
导致应用程序崩溃,我收到此错误:
'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'