2011-02-08 15 views

回答

1

您总是从NSUserDefaults获得NSDictionary的非可变实例。为了使它们变得可变,你需要做这样的事情:

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. 
+0

非常感谢! :) +1 – Ratan 2011-02-08 08:59:25

0

你也可以保存为plist。

这是你如何写一个的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]; 

这就是你如何从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];