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

Jau*_*me 11 iphone xcode cocoa-touch objective-c 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);;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ign*_*ese 6

您的Xcode项目应将您的includes文件夹添加为文件夹参考而不是.

组只是为了保持组织而不是提供文件夹结构,因此在复制到设备时,所有文件最终都处于同一级别.