-2

请帮我。

我有一个 plistclothingList.plist

我在一个方法中像这样访问它

 NSString *path=[[NSBundle mainBundle] pathForResource:@"ClothingList" ofType:@"plist"];
 //NSDictionary *ClothingAssets ; //Declared globally in .h file

  ClothingAssets=[[NSMutableDictionary alloc]initWithContentsOfFile:path];
 [[NSUserDefaults standardUserDefaults]setObject:ClothingAssets forKey:@"ClothingAssets"];
 [[NSUserDefaults standardUserDefaults] synchronize];

现在我想用另一种方法修改服装资产字典中的布尔值。

 ClothingAssets=[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"ClothingAssets"];
 [[[[[[ClothingAssets objectForKey:@"ClothingStore"] objectAtIndex:temp_Store]objectForKey:@"Assets" ]objectForKey:[NSString stringWithFormat:@"%@",temp_AssetType]] objectAtIndex:ii] setValue:@"YES" forKey:@"isLock"] ;

当我第一次运行代码时它崩溃并显示如下错误:

************ Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'***
 *** First throw call stack:
 (0x1fb5012 0x29c5e7e 0x1fb4deb 0x1f7b347 0x3f39bf 0x435d9 0x41f76 0xea5020 0x29d9705 0xab5920 0xab58b8 0xb76671 0xb76bcf 0xb75d38 0xae533f 0xae5552 0xac33aa 0xab4cf8 0x3397df9 0x3397ad0 0x1f2abf5 0x1f2a962 0x1f5bbb6 0x1f5af44 0x1f5ae1b 0x33967e3 0x3396668 0xab265c 0x22ed 0x2225 0x1)
 libc++abi.dylib: terminate called throwing an exception******

但是当我第二次运行代码时,它工作正常。

请帮我。

4

2 回答 2

2

类似的问题还有很多。从异常中可以明显看出您正在尝试将值包含到不可变字典中。

一次打开一个值,因为您要编辑它们,所以总是制作一个 mutableCopy

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableDictionary *clothingAssets=[[defaults dictionaryForKey:@"ClothingAssets"] mutableCopy];

NSMutableArray *clothingStores      = [clothingAssets[@"ClothingStore"] mutableCopy];
NSMutableDictionary *clothingStore  = [clothingStores[temp_Store] mutableCopy];

NSMutableDictionary *assets         = [clothingStore[@"Assets"]mutableCopy];

NSString *assetTypesKey             = [NSString stringWithFormat:@"%@",temp_AssetType];
NSMutableArray *assetTypes          = [assets[assetTypesKey]mutableCopy];
NSMutableDictionary *assetType      = [assetTypes[i] mutableCopy];

//Value is set
assetType[@"isLock"] = @"YES";

//Now you need to update the values back to the top most level
[assetTypes replaceObjectAtIndex:i withObject:assetType];
assets[assetTypesKey] = assetTypes ;
clothingStore[@"Assets"] = assets;
[clothingStores replaceObjectAtIndex:temp_Store withObject:clothingStore];
clothingAssets[@"ClothingStore"] = clothingStores;

[defaults setObject:clothingAssets forKey:@"ClothingAssets"];
[defaults synchronize];
于 2013-06-12T09:00:51.617 回答
0

嵌套消息发送放在一边,这实际上相当简单。当您看到该错误时,这意味着您正在尝试改变一个不可变对象。当您尝试这样做时,运行时会发脾气。相反,请执行以下操作:

ClothingAssets=[[NSUserDefaults standardUserDefaults] dictionaryForKey:@"ClothingAssets"];
NSDictionary *clothingStore = [ClothingAssets objectForKey:@"ClothingStore"];
NSArray *assets = [clothingStore objectForKey:@"Assets"];
NSDictionary *subAsset = [assets objectAtIndex: temp_Store];
NSArray *subAssetArray = [subAsset objectForKey: [NSString stringWithFormat:@"%@",temp_AssetType];

// At this point I'm afraid I run out of creative ways to describe your storage hierarchy:
NSDictionary *subAssetArraySubDictionary = [subAssetArray objectAtIndex: ii];
NSMutableDictionary *mutableCopy = [subAssetArraySubDictionary mutableCopy];

// And finally, set the value:
// Beware: This uses the string YES instead of a boolean value for YES - you need to remember this
// when accessing it later.
[mutableCopy setValue: @"YES" forKey: @"isLock"];
于 2013-06-12T08:47:58.073 回答