Iphone 使用方法setValue:forPath时NSInternalInconsistencyException与NSUserDefaults

Iphone 使用方法setValue:forPath时NSInternalInconsistencyException与NSUserDefaults,iphone,nsuserdefaults,setvalue,Iphone,Nsuserdefaults,Setvalue,我的代码如下: NSMutableDictionary *configure = [[NSUserDefaults standardUserDefaults] objectForKey:@"configure"]; if (!configure){ configure = [NSMutableDictionary dictionary]; [configure setValue:[NSMutableDictionary dictionary] forKeyPath:@"select"]

我的代码如下:

NSMutableDictionary *configure = [[NSUserDefaults standardUserDefaults] objectForKey:@"configure"];
if (!configure){    
configure = [NSMutableDictionary dictionary];
[configure setValue:[NSMutableDictionary dictionary] forKeyPath:@"select"] ;

[configure setValue:[NSMutableDictionary dictionary] forKeyPath:@"select.around"];
[configure setValue: @"All" forKeyPath:@"select.around.name"];

[[NSUserDefaults standardUserDefaults] setObject:configure forKey:@"configure"];
[[NSUserDefaults standardUserDefaults] synchronize];
    }
NSString *keyPath = @"configure.select.around";
NSMutableDictionary *aroundConfigure = [[[NSUserDefaults standardUserDefaults] valueForKeyPath:keyPath] mutableCopy];
[aroundConfigure setObject:@"" forKey:@"name"];
[[NSUserDefaults standardUserDefaults] setValue:aroundConfigure forKeyPath:keyPath];
它每次都会崩溃,错误如下:

** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'
*** Call stack at first throw:
(
    0   CoreFoundation                      0x00daebe9 __exceptionPreprocess + 185
    1   libobjc.A.dylib                     0x00f035c2 objc_exception_throw + 47
    2   CoreFoundation                      0x00d67628 +[NSException raise:format:arguments:] + 136
    3   CoreFoundation                      0x00d6759a +[NSException raise:format:] + 58
    4   CoreFoundation                      0x00dad401 -[__NSCFDictionary setObject:forKey:] + 209
    5   Foundation                          0x0004b34d -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 399
    6   Foundation                          0x0004b34d -[NSObject(NSKeyValueCoding) setValue:forKeyPath:] + 399
    7   Untitled                            0x00002429 -[UntitledAppDelegate application:didFinishLaunchingWithOptions:] + 774
    8   UIKit                               0x002b81fa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1163
    9   UIKit                               0x002ba55e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 439
    10  UIKit                               0x002c4db2 -[UIApplication handleEvent:withNewEvent:] + 1533
    11  UIKit                               0x002bd202 -[UIApplication sendEvent:] + 71
    12  UIKit                               0x002c2732 _UIApplicationHandleEvent + 7576
    13  GraphicsServices                    0x016e4a36 PurpleEventCallback + 1550
    14  CoreFoundation                      0x00d90064 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
    15  CoreFoundation                      0x00cf06f7 __CFRunLoopDoSource1 + 215
    16  CoreFoundation                      0x00ced983 __CFRunLoopRun + 979
    17  CoreFoundation                      0x00ced240 CFRunLoopRunSpecific + 208
    18  CoreFoundation                      0x00ced161 CFRunLoopRunInMode + 97
    19  UIKit                               0x002b9fa8 -[UIApplication _run] + 636
    20  UIKit                               0x002c642e UIApplicationMain + 1160
    21  Untitled                            0x00002100 main + 102
    22  Untitled                            0x00002091 start + 53
)
terminate called after throwing an instance of 'NSException'


我知道
setObject:forKey:
应该与可变命令一起使用。将KVO与NSUserDefaults一起使用是否有任何问题?顺便说一句,我使用的平台是iPhone。

您的代码没有做您认为它正在做的事情:

NSMutableDictionary *configure = [[NSUserDefaults standardUserDefaults] objectForKey:@"configure"];
这将
configure
设置为标准值,默认值为键
configure

configure = [NSMutableDictionary dictionary];
这会将
configure
设置为一个新的空字典,而不是用户默认设置的字典

[configure setValue:[NSMutableDictionary dictionary] forKeyPath:@"select"] ;
现在将一个空字典作为键的值
select

[configure setValue:[NSMutableDictionary dictionary] forKeyPath:@"select.around"];
您正在为此密钥路径添加另一个空字典

[configure setValue: @"All" forKeyPath:@"select.around.name"];
[[NSUserDefaults standardUserDefaults] setObject:configure forKey:@"configure"];
您正在为此键路径设置字符串值

[configure setValue: @"All" forKeyPath:@"select.around.name"];
[[NSUserDefaults standardUserDefaults] setObject:configure forKey:@"configure"];
您现在正在将这本奇怪的词典放入NSUserDefaults中

[[NSUserDefaults standardUserDefaults] synchronize];

NSString *keyPath = @"configure.select.around";

NSMutableDictionary *aroundConfigure = [[[NSUserDefaults standardUserDefaults] valueForKeyPath:keyPath] mutableCopy];
这只是一本空的可变字典

[aroundConfigure setObject:@"" forKey:@"name"];
[[NSUserDefaults standardUserDefaults] setValue:aroundConfigure forKeyPath:keyPath];
您正在为此字典中的某个键设置空字符串

[aroundConfigure setObject:@"" forKey:@"name"];
[[NSUserDefaults standardUserDefaults] setValue:aroundConfigure forKeyPath:keyPath];
然后将这个奇怪的字典设置为用户默认值

这是非常令人困惑的

你想做什么

编辑以添加

如果您已经“简化”了代码,那么提供代码是没有意义的,因为我们看不到它应该做什么

您的应用程序正在崩溃,因为您正试图更改NSDictionary的键值对,它是不可变的,而不是NSMutableDictionary。请参阅跟踪的第6行


由于您提供了“简化”代码,我不知道您在哪里执行此操作。

今天,我也面临此问题,因为我在字典中生成的数据是这样的

NSMutableDictionary *Dic =[myMutableAry objectAtIndex:0];  
当我更新dic值时获取数据,然后崩溃

*由于未捕获异常“NSInternalInconsistencyException”而终止应用程序,原因:'-[\uu NSCFDictionary setObject:forKey:::发送到不可变对象的mutating方法

经过大量研究,我确实找到了解决方案,所以只需做一件事

NSMutableDictionary *Dic = [NSMutableDictionary dictionaryWithDictionary:[myMutableAry objectAtIndex:0]];

使用NSUserDefaults的方式是错误的,NSUserDefaults返回一个不可变的字典。这就是这个问题的原因。因此,您必须显式地从NSUserDefaults请求可变对象。请使用下面的代码行

NSMutableDictionary *configure = [[[NSUserDefaults standardUserDefaults] objectForKey:@"configure"]mutableCopy];
这是我的工作。
感谢好运:)

hi,abizern,我打算在app的init处创建一个字典{“configure”:{“select”:{“around”:{“around”:{“name”:“All”}}}}}},然后将keypath configure.sellect.around.name设置为空字符串。我简化了代码。所以这看起来毫无意义。我只想知道它为什么会崩溃。谢谢,一起来吧,我想这是你应得的。我所需要的只是“你的应用程序正在崩溃,因为你正在尝试更改NSDictionary的键值对,它是不可变的,而不是NSMutableDictionary”ddddd uuuuuhhhh