【问题标题】:Strange bug when renaming a file with NSFileManager使用 NSFileManager 重命名文件时出现奇怪的错误
【发布时间】:2011-04-10 14:14:17
【问题描述】:

我曾经使用以下内容来重命名具有各种用户输入值的 aplist 文件(主要是 BOOL,字符串):

    [manager moveItemAtPath:oldPath toPath:newPath error:&error];

由于某些原因在 iOS4+ 中出现了故障,所以我尝试了以下操作:

 [1]  [manager copyItemAtPath:oldPath toPath:newPath error:&error];
 [2]  [manager removeItemAtPath:oldPath error:&error];

现在,[1] 正确复制了 plist 和所有数据。 (我在注释掉 [2] 时已验证。但由于某种原因,当我尝试 [1] 和 [2] 时,文件 /appears/ 已重命名,但是,只有某些字段丢失,例如 I FName 和 LName保持不变,而 Phone1、Phone2、Email1、Email2 和 Website 将从新文件中删除。

对于我的生活,我无法弄清楚为什么在创建新文件后尝试删除旧文件的 removeFileAtPath 时只保留一些数据?

任何帮助将不胜感激!

我只是想重命名一个 plist 文件,同时保持所有数据完好无损。

【问题讨论】:

    标签: file copy plist rename nsfilemanager


    【解决方案1】:
    NSString *newPath = [[oldPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newFilename];
    
    [[NSFileManager defaultManager] movePath:oldPath toPath:newPath handler:nil];
    

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2015-04-01
      • 2014-11-26
      • 2020-08-30
      • 2013-05-05
      • 2021-10-12
      相关资源
      最近更新 更多