【问题标题】:iPhone (iOS): copying files from main bundle to documents folder erroriPhone (iOS):将文件从主包复制到文档文件夹错误
【发布时间】:2011-03-15 20:06:04
【问题描述】:

我试图在首次启动时将一些文件从我的应用程序包复制到文档目录。我有第一次启动的检查,但为了清楚起见,它们没有包含在代码 sn-p 中。问题是我正在复制到文档目录(已经存在)并且在文档中,它指出:

dstPath 在操作之前不得存在。

实现直接复制到文档根目录的最佳方法是什么?我想这样做的原因是允许 iTunes 文件共享支持。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
  NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Populator"];

  NSLog(@"\nSource Path: %@\nDocuments Path: %@", sourcePath, documentsDirectory);

  NSError *error = nil;

  if([[NSFileManager defaultManager] copyItemAtPath:sourcePath toPath:documentsDirectory error:&error]){
    NSLog(@"Default file successfully copied over.");
  } else {
    NSLog(@"Error description-%@ \n", [error localizedDescription]);
    NSLog(@"Error reason-%@", [error localizedFailureReason]);
  }
  ...
  return YES;
}

谢谢

【问题讨论】:

    标签: iphone nsfilemanager


    【解决方案1】:

    您的目标路径必须包含要复制的项目的名称,而不仅仅是文档文件夹。试试:

    if([[NSFileManager defaultManager] copyItemAtPath:sourcePath 
              toPath:[documentsDirectory stringByAppendingPathComponent:@"Populator"]
              error:&error]){
    ...
    

    编辑:抱歉误解了您的问题。不知道是否有更好的选择,然后遍历文件夹内容并分别复制每个项目。如果你的目标是 iOS4,你可以使用 NSArray 的 -enumerateObjectsUsingBlock: 函数:

    NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:copyItemAtPath:sourcePath error:NULL];
    [resContents enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        {
            NSError* error;
            if (![[NSFileManager defaultManager] 
                      copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj] 
                      toPath:[documentsDirectory stringByAppendingPathComponent:obj]
                      error:&error])
                DLogFunction(@"%@", [error localizedDescription]);
        }];
    

    附:如果你不能使用块,你可以使用快速枚举:

    NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:copyItemAtPath:sourcePath error:NULL];
    
    for (NSString* obj in resContents){
        NSError* error;
        if (![[NSFileManager defaultManager] 
                     copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj] 
                     toPath:[documentsDirectory stringByAppendingPathComponent:obj]
                     error:&error])
                DLogFunction(@"%@", [error localizedDescription]);
        }
    

    【讨论】:

    • 感谢您的回复,但这不是我想做的。我想要做的是将 Populator 文件夹的内容复制到目录根目录(不是文档根目录中名为 Populator 的文件夹)。您所说的确实有效,但不是我想要实现的目标。
    • 非常感谢,误会几乎肯定是我的错。我将此代码用于 iPad 应用程序,因此将针对 iOS 3.2,尽管最终这将支持 iOS4。感谢您的代码,知道如何让它适用于 3.2(无块)吗?
    • 我使用快速枚举添加了代码。积木解决方案更多是为了锻炼自己——积木对我来说是一个新概念。
    • 非常感谢,非常感谢您的努力。
    【解决方案2】:

    备注:
    不要在 didFinishLaunchingWithOptions: 中发出冗长的操作是一个概念错误。 如果这个副本花费太多时间,看门狗会杀了你。 在辅助线程或 NSOperation 中启动它... 我个人使用计时器。

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 1970-01-01
      • 2011-05-02
      • 2013-09-03
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多