iOS Plist每次保存时都会被覆盖

时间:2013-07-10 16:23:13

标签: ios objective-c plist nsdictionary nsmutabledictionary

使用plist在iOS App中保存和加载基本变量。我遇到的问题是每次保存新数据时我的plist似乎都被删除了。我已经在这方面苦苦挣扎了几天,我确信必须有一个简单的解决方案。下面是我的SaveData和LoadData的代码。谢谢!

- (void) saveData{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: age, activityLevel, activityLevelFactorNumber, nil] forKeys:[NSArray arrayWithObjects: @"Age", @"ActivityLevel", @"ActivityLevelFactor", nil]];

NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

    // write plistData to our Data.plist file
    [plistData writeToFile:plistPath atomically:YES];

加载数据(我只能从保存的最后一个plist加载):

- (void) LoadData {
  // get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];

// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
    // if not in documents, get property list from main bundle
    plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}

// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];

1 个答案:

答案 0 :(得分:0)

我会更改你的LoadData方法,以便它返回加载的数据(而不是将其加载到实例变量或当前的任何变量)。另外,更正约定的命名:

- (NSDictionary *)loadData { ...

我还将路径代码重构为自己的方法:

- (NSString *)pathForSavingData { ...

然后,当你来保存时:

- (void)appendAndSaveData {
    // get the path to our Data/plist file
    NSString *plistPath = [self pathForSavingData];

    // create dictionary with values in UITextFields
    NSMutableDictionary *plistDict = [[self loadData] mutableCopy];

    NSDictionary *newEntries = [NSDictionary dictionaryWithObjects:@[age, activityLevel, activityLevelFactorNumber, nil]
                                                           forKeys:@[@"Age", @"ActivityLevel", @"ActivityLevelFactor", nil]];

    [plistDict addEntriesFromDictionary:newEntries];

    // write plistData to our Data.plist file
    [plistData writeToFile:plistPath atomically:YES];
}