【问题标题】:Unable to write to plist file无法写入 plist 文件
【发布时间】:2011-10-08 16:56:41
【问题描述】:

我在网站上发现了几个有类似问题的人的问题,但我仍然无法解决我的问题。事情是这样的:

我正在制作一个包含两个 plist 文件的 iOS 应用程序。当用户第一次打开应用程序时,应用程序负责将这两个文件移动到 Documents 目录。我知道这段代码有效,因为我随后能够从 Documents 目录路径中的每个文件中读取。但是,由于明显的未知原因,我无法写信给他们中的任何一个。这是我用来写入第一个 plist 文件的代码:

+ (void)insertNewElementWith:(NSArray *)objects andKeys:(NSArray *)keys {
    NSString *filePath = [self returnPathForFirstPlistFile];
    NSMutableDictionary *contentOfFirstPlistFile = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
    [contentOfFirstPlistFile setObject:dictionary forKey:[NSNumber numberWithInt:[contentOfFirstPlistFile count]]];
    [contentOfFirstPlistFile writeToFile:filePath atomically:YES];
    [dictionary release];
}

returnPathForFirstPlistFile 确实工作,因为contentOfFirstPlistFile 包含{ } 而不是null。但是,任何人都可以向我解释为什么它一直返回{ },即使上述方法已被多次调用。

更新

这是我用来将文件放在 Documents 目录中的代码。 placePlistsInDocumentsDirectory在用户通过应用登录时调用,因此这只发生一次。

+ (NSString *)returnPathForFirstPlistFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"First.plist"];

    return filePath;
}

+ (NSString *)returnPathForSecondPlistFile {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSLog(@"%@", [paths objectAtIndex:0]);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Second.plist"];

    return filePath;
}

+ (void)placePlistsInDocumentsDirectory {
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *firstPlistPath = [self returnPathForFirstPlistFile];
    NSString *secondPlistPath = [self returnPathForSecondPlistFile];

    if (![fileManager fileExistsAtPath:firstPlistPath]) {
        NSError *error;
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"First" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:firstPlistPath error:&error];
    }

    if (![fileManager fileExistsAtPath:secondPlistPath]) {
        NSError *error;
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Second" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:secondPlistPath error:&error];
    }
}

【问题讨论】:

    标签: ios plist nsdictionary nsmutabledictionary writetofile


    【解决方案1】:
    [NSNumber numberWithInt:[contentOfFirstPlistFile count]]
    

    给你一个 NSNumber 对象。这不是字典的有效键,键必须是 NSString。使用

    [NSString stringWithFormat:@"%d",[contentOfFirstPlistFile count]]
    

    相反。但是,如果您在字典中使用数字作为键,为什么不使用数组呢?

    我试图自己重现这个问题,我能解决它的唯一方法是使用一个没有字典作为其根对象的 plist。这返回了一个不为零的字典,但不允许任何修改。然后无法将其写回文件。查看您之前的一些问题,您之前使用的是基于数组的 plist,所以这可能是问题所在。

    【讨论】:

    • 谢谢,但还是不行。将 BOOL 变量应用于writeToFile 行时,即使我已将 NSNumber 替换为 NSString,它也会返回负数。我也尝试过使用数组,但这也不起作用。
    • 如果您在添加dictionary 之前和之后记录contentOfFirstPlistFile,您会看到什么?
    • 前面是空字典,后面是内容字典。
    • 好的,您确定您没有使用“首次打开应用程序”代码再次复制该文件吗?如果在该方法中,您通过读取路径创建一个新字典,它包含什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    • 2018-08-25
    • 2013-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多