Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iPhone(iOS):将文件从主捆绑包复制到文档文件夹错误_Iphone_Nsfilemanager - Fatal编程技术网

iPhone(iOS):将文件从主捆绑包复制到文档文件夹错误

iPhone(iOS):将文件从主捆绑包复制到文档文件夹错误,iphone,nsfilemanager,Iphone,Nsfilemanager,我试图在第一次启动时将一些文件从我的应用程序包复制到文档目录。我已经为第一次发布做好了检查,但为了清晰起见,代码片段中没有包含这些检查。问题是我正在复制到documents目录(已经存在),并且在文档中,它声明: 操作之前,dstPath不得存在 对于我来说,实现直接复制到文档根目录的最佳方法是什么?我想这样做的原因是为了支持iTunes文件共享 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOpti

我试图在第一次启动时将一些文件从我的应用程序包复制到文档目录。我已经为第一次发布做好了检查,但为了清晰起见,代码片段中没有包含这些检查。问题是我正在复制到documents目录(已经存在),并且在文档中,它声明:

操作之前,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;
}

谢谢

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

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]);
    }
注:
不要在didFinishLaunchingWithOptions中发出冗长的操作:这是一个概念上的错误。 如果这个副本花费太多时间,看门狗会杀了你。 在辅助线程或NSO操作中启动它。。。
我个人使用定时器程序。

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