Спасибо, Брукс. Ваш вопрос заставил меня продолжать копаться в том, существовал ли файл вообще в моем пакете -, а его не было!
Таким образом, используя этот код (также ниже):iPhone/iPad:Невозможно скопировать папку из NSBundle в NSDocumentDirectory и инструкции по правильному добавлению каталога в Xcode (из здесь и ниже)Мне удалось заставить его работать.
Скопируйте папку в Xcode:
Каталог должен стать синим, а не желтым.
-(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);
}
}
}
===================== === КОНЕЦ РЕДАКТИРОВАТЬ
Фрус-блей-ши-на! В любом случае...
Приведенный ниже код отлично копирует мою папку из комплекта приложений в папку «Документы» в симуляторе. Однако на устройстве я получаю сообщение об ошибке и отсутствие папки. С помощью гугля выяснил, что ошибка (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 ;
}