writeToFile NSDictionary没有正确保存到plist

时间:2014-07-02 01:57:07

标签: ios nsdictionary plist

我有一个自定义plist用于填充UItableViewCells,我能够完美地读取它们,但是当我尝试写入我的自定义plist文件时,它永远不会改变。

NSString *errorDesc = nil;
NSString * plistPath = [[NSBundle mainBundle] pathForResource:@"AdvanceSearchPrefrences" ofType:@"plist"];
NSMutableDictionary *advPrefs = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[advPrefs setObject:cell.textLabel.text forKey:@"Manuf"];
[advPrefs setObject:selRow forKey:@"ManufNum"];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:advPrefs format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
[plistData writeToFile:plistPath atomically:YES];


NSString * plistPath2 = [[NSBundle mainBundle] pathForResource:@"AdvanceSearchPrefrences" ofType:@"plist"];
NSMutableDictionary *advPrefs2 = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath2];

advPrefs显示新值,advPrefs2显示旧值。

2 个答案:

答案 0 :(得分:2)

您无法直接保存在plist上,但您可以创建副本并将其保存到NSUserDefaults。

在初始加载时,您可以在AppDelegate中执行此类操作。这会将您的plist复制到您可以编辑和保存的内容中:

    NSString * plistPath = [[NSBundle mainBundle] pathForResource:@"AdvanceSearchPrefrences" ofType:@"plist"];
    NSMutableDictionary *advPrefs = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:advPrefs] forKey:@"plist"];
    [[NSUserDefaults standardUserDefaults] synchronize];

在您想要获取复制的plist的类中,您可以调用以下内容:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *data = [defaults objectForKey:@"plist"];
    NSMutableDictionary *advPrefs = [[NSKeyedUnarchiver unarchiveObjectWithData:data]mutableCopy];

然后进行更改

    [advPrefs setObject:cell.textLabel.text forKey:@"Manuf"];
    [advPrefs setObject:selRow forKey:@"ManufNum"];

然后将它们保存到NSUserDefaults

    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:advPrefs] forKey:@"plist"];
    [[NSUserDefaults standardUserDefaults] synchronize];

我的应用程序遇到了同样的问题,这就是我修复它的方法。希望这也适合你

答案 1 :(得分:0)

阅读数据:

NSData *data;
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"AdvanceSearchPrefrences.plist"];
if (filePath) {
    data = [[NSString stringWithContentsOfFile:filePath
                                      encoding:NSUTF8StringEncoding
                                         error:&error]
                             dataUsingEncoding:NSUTF8StringEncoding];
    // use data here....
}

写数据:

NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [documentDirectoryPath stringByAppendingPathComponent:@"AdvanceSearchPrefrences.plist"];
if ([dataToBeWritten writeToFile:path
                      atomically:YES
                        encoding:NSUTF8StringEncoding
                           error:&error]) {
    // data is written
}