【发布时间】:2012-04-07 11:21:59
【问题描述】:
编辑:已解决
谢谢布鲁克斯。您的问题使我继续深入研究该文件是否存在于我的捆绑包中 - 但它不存在!
因此,通过使用此代码(也在下方):iPhone/iPad:无法将文件夹从 NSBundle 复制到 NSDocumentDirectory 以及将目录正确添加到 Xcode 的说明(来自here 及以下)我能够得到它工作。
将文件夹复制到 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); } }}
========================= 结束编辑
Frus-stray-shee-on!总之……
下面的代码将我的文件夹从 app bundle 复制到模拟器中的 Documents 文件夹中。但是在设备上我得到一个错误并且没有文件夹。使用 ze Google 我发现错误(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 ;
}
【问题讨论】:
-
这:
if ( !( [ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&error ]))不合逻辑。您正在检查该方法是否存在,而不是它是否成功。
标签: iphone ios copy bundle nsfilemanager