0

使用 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];
4

1 回答 1

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];
}
于 2013-07-10T18:35:30.373 回答