Iphone iOS将文件从主捆绑包复制到文档目录
我正在尝试将添加到名为“includes”文件夹中的文件复制到文档目录中名为“includes”的文件夹中 对于Iphone iOS将文件从主捆绑包复制到文档目录,iphone,objective-c,ios,xcode,cocoa-touch,Iphone,Objective C,Ios,Xcode,Cocoa Touch,我正在尝试将添加到名为“includes”文件夹中的文件复制到文档目录中名为“includes”的文件夹中 对于重内容,我得到一个nil值。为什么? - (void)copyResources{ NSString *sourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"includes"]; NSString *destPath = [[self applicati
重内容
,我得到一个nil值。为什么?
- (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);;
}
}
}
查看已编译的应用程序包 通常,Xcode生成的bundle是扁平的。这意味着,尽管您添加的资源文件将复制到捆绑包中,但您创建的任何目录都不会复制到捆绑包中,因此资源路径中没有“includes”目录。因此,您的源内容将为零 因此,在您的情况下,尝试仅使用:
NSString*sourcePath=[[NSBundle mainBundle]resourcePath]代码>
编辑:显然,添加文件夹引用也很有效(归功于Ignacio Inglese)。您的Xcode项目应该将您的包含的文件夹添加为文件夹引用,而不是作为一个组
组只是用来组织事物,而不是提供文件夹结构,因此,当复制到设备时,所有文件都以相同的级别结束。ok,但列出了所有文件,包括.nib和plists!我只需要从特定文件夹中选择文件。不可能?它是捆绑包中的一个平面结构。@Jaume那么您将必须选择所需的文件(可能是通过公共扩展名,或者在代码中保留一个文件名列表)。