iphone - 将文件夹(带内容)从 bundle 复制到文档目录 - iOS

标签 iphone ios copy bundle nsfilemanager

编辑:已解决

谢谢布鲁克斯。你的问题让我继续深入研究该文件是否存在于我的包中 - 但它不存在!

因此,通过使用此代码(也在下方):iPhone/iPad:无法将文件夹从 NSBundle 复制到 NSDocumentDirectory 以及将目录正确添加到 Xcode 的说明(来自 here 及以下)我能够让它工作。

将文件夹复制到 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);
        }
    }
    

======================== 结束编辑

Frus-stray-shee-on!无论如何...

下面的代码将我的文件夹从应用程序包复制到模拟器中的 Documents 文件夹就可以了。但是在设备上出现错误并且没有文件夹。使用 ze Google 我发现错误 (260) 表示文件(在本例中是我的文件夹)不存在。

可能出了什么问题?为什么我不能将我的文件夹从 bundle 复制到文档?我已经检查过文件是否存在——尽管文件夹没有显示——因为 Xcode 想要平面文件?它是否将我的文件夹(拖到 Xcode 中)变成了 Assets 的平面文件?

感谢您的帮助。

//  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-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;
            }
        }
    }
}

关于iphone - 将文件夹(带内容)从 bundle 复制到文档目录 - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9830061/

相关文章:

iphone - 子类化 UIButton 以创建自定义外观按钮有什么意义吗?

ios - 每一个新行添加一个编号列表 UITextView

MongoDB 使用外键将字段复制到另一个集合

iphone - 为什么 XCode 每次都会将资源复制到 iDevice,即使它们仍未更改?

ios - 带有图像和文本的 UIButton 可能吗?

iphone - 不同上下文中对象之间的非法关系 : but I only have one context?

iphone - SQLite 核心数据性能随时间的变化

ios - 在没有预处理器宏的情况下,有没有办法在 Xcode 项目的项目级别定义实用的方案特定标志

ios - GitHub 操作 IOS xcode QA/Staging 和证书和配置文件问题

java - 在 Java 中将文件从一个目录复制到另一个目录