i have a plist that's at its root an array with dictonaries inside it.
i load a plist from my recourses as an NSMutableArray.
[NSMutableArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Filters" ofType:@"plist"]]
i store it into nsuserdefault because it has to be persistent between startups.
[[NSUserDefaults standardUserDefaults] setObject:array forKey:@"filters"];
but i can't change the dictonaries in the array b开发者_StackOverflow中文版ecause they are not mutable. how can i make them mutable?
Check out using "mutabilityOption:NSPropertyListMutableContainersAndLeaves", this gives you very precise control over which elements are added as static and which elements are added as mutable. From the property list programers guide:
If you need finer-grained control over the mutability of the objects in a property list, use the propertyListFromData:mutabilityOption:format:errorDescription: class method, whose second parameter permits you to specify the mutability of objects at various levels of the aggregate property list. You could specify that all objects are immutable (NSPropertyListImmutable), that only the container (array and dictionary) objects are mutable (NSPropertyListMutableContainers), or that all objects are mutable (NSPropertyListMutableContainersAndLeaves).
For example, you could write code like this:
NSMutableArray *dma = (NSMutableArray *)[NSPropertyListSerialization
propertyListFromData:plistData
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:&format
errorDescription:&error];
This call produces a mutable array (dma) with mutable dictionaries in each element. Each key and each value in each dictionary are themselves also mutable.
You can replace the dictionary by a mutable copy of itself, using the 'mutableCopy' method of NSDictionary.
[EDIT] Example:
[ array replaceObjectAtIndex: 42 withObject: [ [ [ array objectAtIndex: 42 ] mutableCopy ] autorelease ] ];
精彩评论