编辑:已解决
感谢布鲁克斯。如果文件存在于我的软件包中,您的问题让我继续深入研究 - 但事实并非如此!因此,通过使用此代码(也在下面):iPhone/iPad:无法将文件夹从NSBundle复制到NSDocumentDirectory以及正确添加目录到Xcode的指令(从here及以下)我能够使它工作。从文件夹复制文件夹(带内容)到Documents目录 - iOS
文件夹复制到Xcode中:
- 你的Mac上创建一个目录。
- 选择添加现有文件到您的项目
- 选择要导入
- 在弹出的窗口中请务必选择“复制物品进入 目的地组的文件夹”和“创建任何 文件夹引用目录添加文件夹“
- 点击”添加“
该目录应该显示为蓝色而不是黄色。
-(void) copyDirectory:(NSString *)directory { NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory]; NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory]; if (![fileManager fileExistsAtPath:documentDBFolderPath]) { //Create Directory! [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&error]; } else { NSLog(@"Directory exists! %@", documentDBFolderPath); } NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&error]; for (NSString *s in fileList) { NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s]; NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s]; if (![fileManager fileExistsAtPath:newFilePath]) { //File does not exist, copy it [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&error]; } else { NSLog(@"File exists: %@", newFilePath); } }
}
======================== END EDIT
的FRU-stray- SHEE-ON!无论如何...
下面的代码将我的文件夹从应用程序包复制到模拟器中的文档文件夹就好了。但是,在设备上,我收到一个错误,没有文件夹。使用泽谷歌我发现错误(260)意味着文件(在这种情况下,我的文件夹)不存在。
可能会出现什么问题?为什么我不能将我的文件夹从软件包复制到文档?我检查过这些文件存在 - 尽管文件夹没有显示 - 因为Xcode需要平面文件?它是否将我的文件夹(拖入Xcode)转换为资产的平面文件?
我感谢您的任何帮助。
// Could not copy report at path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery to path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/Documents/plans.gallery. error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. (Cocoa error 260.)" UserInfo=0x365090 {NSFilePath=/var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery, NSUnderlyingError=0x365230 "The operation couldn’t be completed. No such file or directory"}
NSString *resourceDBFolderPath;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"];
BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath];
if (success){
NSLog(@"Success!");
return;
} else {
resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"plans.gallery"];
[fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
//[fileManager createDirectoryAtURL:documentDBFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
[fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath
error:&error];
}
//check if destinationFolder exists
if ([ fileManager fileExistsAtPath:documentDBFolderPath])
{
//removing destination, so source may be copied
if (![fileManager removeItemAtPath:documentDBFolderPath error:&error])
{
NSLog(@"Could not remove old files. Error:%@",error);
return;
}
}
error = nil;
//copying destination
if (!([ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ]))
{
NSLog(@"Could not copy report at path %@ to path %@. error %@",resourceDBFolderPath, documentDBFolderPath, error);
return ;
}
这样的:'如果(([文件管理copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath错误:错误]))'是不合逻辑的。您正在检查方法是否存在,而不是如果成功。 – CodaFi 2012-03-22 20:48:54