2012-03-22 317 views
12

编辑:已解决

感谢布鲁克斯。如果文件存在于我的软件包中,您的问题让我继续深入研究 - 但事实并非如此!因此,通过使用此代码(也在下面):iPhone/iPad:无法将文件夹从NSBundle复制到NSDocumentDirectory以及正确添加目录到Xcode的指令(从here及以下)我能够使它工作。从文件夹复制文件夹(带内容)到Documents目录 - iOS

文件夹复制到Xcode中:

  1. 你的Mac上创建一个目录。
  2. 选择添加现有文件到您的项目
  3. 选择要导入
  4. 在弹出的窗口中请务必选择“复制物品进入 目的地组的文件夹”和“创建任何 文件夹引用目录添加文件夹“
  5. 点击”添加“
  6. 该目录应该显示为蓝色而不是黄色。

    -(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 ; 
} 
+1

这样的:'如果(([文件管理copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath错误:错误]))'是不合逻辑的。您正在检查方法是否存在,而不是如果成功。 – CodaFi 2012-03-22 20:48:54

回答

10

我冒昧编辑了一些我觉得需要一点家务的代码。你正在使用不推荐使用的方法,过于复杂的方法,只是简单的荒谬if-elses。当然,我会检查你的文件路径是否有效,不知道.gallery文件是什么,并且不提供虚拟文件,我唯一能做的判断就是你的文件路径是无效的,因为资源不会不存在你认为它确实存在的地方。 (在一个点上,你问到的文件复制到文档目录,然后检查它是否在您的包存在!)

-(void)testMethod { 

    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 { 
     //simplified method with more common and helpful method 
     resourceDBFolderPath = [[NSBundle mainBundle] pathForResource:@"plans" ofType:@"gallery"]; 

     //fixed a deprecated method 
     [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:nil]; 

     [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath   
           error:&error]; 

     //check if destinationFolder exists 
     if ([ fileManager fileExistsAtPath:documentDBFolderPath]) 
     { 
      //FIXED, another method that doesn't return a boolean. check for error instead 
      if (error) 
      { 
       //NSLog first error from copyitemAtPath 
       NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); 

       //remove file path and NSLog error if it exists. 
       [fileManager removeItemAtPath:documentDBFolderPath error:&error]; 
       NSLog(@"Could not remove old files. Error:%@", [error localizedDescription]); 
       return; 
      } 
     } 
    } 
} 
+0

欣赏评论/家务。 – malaki1974 2012-03-22 21:12:38

+1

没问题!这就是我的生活。 – CodaFi 2012-03-22 21:18:07

相关问题