Reputation: 1757
I used NSUserDefaults to save NSMutableDictionary. But when I am retrieving the value from the NSUserDefaults, I am unbale to modify/update the values of the NSMutableDictionary into which I am saving the values.
Need some suggestion on how to do so?
Upvotes: 0
Views: 350
Reputation: 519
You can also save to a plist.
This is how you write to a plist:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docDirectory = [paths objectAtIndex:0];
NSString * dataFilePath = [docDirectory stringByAppendingPathComponent:@"YourFile.dat"]
NSMutableDictionary * newLocalPlist = [[NSMutableDictionary alloc] init];
// add your prefs here
[newLocalPlist writeToFile:dataFilePath atomically:YES];
[newLocalPlist release];
And this is how you read from a plist:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * docDirectory = [paths objectAtIndex:0];
NSString * dataFilePath = [docDirectory stringByAppendingPathComponent:@"YourFile.dat"]
NSMutableDictionary * lastLocalPlist = [[[NSMutableDictionary alloc] initWithContentsOfFile:dataFilePath] autorelease];
Upvotes: 0
Reputation: 92316
You always get non-mutable instances of NSDictionary
from NSUserDefaults
. To make them mutable, you need to do something like this:
dict = [[NSUserDefaults standardUserDefaults] objectForKey:@"myKey"];
myMutableDict = [dict mutableCopy];
// Note that the retain count is +1, so you will need to
// release or autorelease myMutableDict later on.
Upvotes: 1