iOS从主包复制文件到文件目录

我正在试图将我添加到名为“includes”的文件夹复制到名为“includes”的文档目录中的文件夹。 我得到resContents零值。 为什么? 谢谢

- (void)copyResources{ NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"includes"]; NSString *destPath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent:@"includes"]; NSArray* resContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:NULL]; for (NSString* obj in resContents){ NSError* error; if (![[NSFileManager defaultManager] copyItemAtPath:[sourcePath stringByAppendingPathComponent:obj] toPath:[destPath stringByAppendingPathComponent:obj] error:&error]) NSLog(@"Error: %@", error);; } } 

您应该将您的includes文件夹添加到您的Xcode项目作为文件夹引用而不是组。

组只是为了保持组织的东西,而不是提供一个文件夹结构,因此,当复制到设备,所有的文件最终在同一水平。

看看你的编译应用程序包。

通常,XCode生成的包是平的。 这意味着,虽然您添加的资源文件将被复制到捆绑包中,但您创build的任何目录都不会,因此资源path中没有“包含”目录。 因此,您的源内容将为零。 所以在你的情况下,尝试只使用`NSString * sourcePath = [[NSBundle mainBundle] resourcePath];

编辑:好吧,显然添加文件夹引用也可以(信贷到伊格纳西奥英语)。