【问题标题】:NSFileManager creating folders herarichy & not saving plist [open]NSFileManager 创建文件夹层次结构 & 不保存 plist [打开]
【发布时间】:2012-09-24 10:26:13
【问题描述】:

问题出现在设备上而不是模拟器上。

 NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Plist1.plist"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSLog(@"documentsDirectory  --- %@", documentsDirectory);
    NSLog(@"path --- %@", path);
    @try
    {
        if (![fileManager fileExistsAtPath:path])
        {
            [fileManager copyItemAtPath:documentsDirectory toPath:path error:&error];
        }
    }
    @catch (NSException *exception)
    {
        NSLog(@" [exception description] -- %@", [exception description]);
    }
    @finally {

    }

    [dictEmot writeToFile:path atomically:YES];

    // To verify
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    NSLog(@"[dict allKeys] My plist ----- %@", [dict allKeys]); 

上面是我编写的用于在我的文档目录中保存两个 plist 文件的代码。我用来保存第二个 plist 的方法相同。

[self savePlist:plist1];
[self savePlist:plist2];

我的问题是,每当我尝试保存第二个 plist 时,它会在文档目录中创建文件夹层次结构,并且不会保存我的 plist2 文件及其内容。

一旦完成第二个方法调用,我的应用文档目录如下所示,

Documents
 -> plist1
   -> plist1
      .
      .
      .
     -> plist1
       -> plist1
other files

我也很喜欢在主线程上做,但结果相同。

它甚至没有打印异常。

  • 我的错误是什么?

    - 为什么要创建层次结构?

【问题讨论】:

    标签: ios plist nsfilemanager nsdocumentdirectory


    【解决方案1】:

    我对 iOS 不太了解,但我认为您的问题出在 stringByAppendingPathComponent:@"Plist1.plist" 检查您的文档,因为我对 Appending 这个词很怀疑,即使您发送新文件名,因为您将文件名的值硬编码到plist1.plist...

    我仍然不确定,因为我已经很长时间没有做 iOS 编码了。

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      • 2017-10-23
      • 2011-09-26
      相关资源
      最近更新 更多