【问题标题】:iOS - NSMutableDictionary writeToFile returns YES but plist is not modifiediOS - NSMutableDictionary writeToFile 返回 YES 但 plist 未修改
【发布时间】:2014-11-30 07:29:12
【问题描述】:

我正在尝试将 NSMutableDictionary 中的数据保存到 plist。 Plist 已经在 Resources 文件夹中。当我尝试使用以下代码将数据保存到 plist 时,它返回 YES,这意味着数据已成功保存。但是当我检查文件数据时没有保存到它。

    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"userInfo.plist"]; //3
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if (![fileManager fileExistsAtPath: path]){ //4    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"userInfo" ofType:@"plist"]; //5
        [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
    }

    BOOL b = [self.userInfoArray writeToFile:path atomically: YES];
    NSLog(@"status %i ", b);

谁有解决办法?

【问题讨论】:

  • 我确定。我通过放置一个调试点进行了检查。
  • 您无法写入资源包。
  • 您是否 100% 确定您正在查看的文件与正在写入的文件相同?
  • rmaddy - 你能解释一下你的答案吗
  • 为什么要从bundle中复制plist,然后马上用数组覆盖?

标签: ios plist nsmutabledictionary writetofile


【解决方案1】:

我想我看错了文件。 Xcode 中的 Plist 文件在保存数据后不会改变。相反,模拟器中的 plist 正在发生变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多