无法将文件从Resources文件夹复制到Documents文件夹

时间:2013-04-17 16:58:51

标签: ios

enter image description here

这是我的代码:

- (void)copyFileIfNeeded
{
    NSFileManager *fileManager= [NSFileManager defaultManager];
    NSError *error;
    NSString *filePath = [self getFile];
   BOOL success = [fileManager fileExistsAtPath:filePath];
   if ( !success )
   {
        NSString *defaultFilePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"BQWDatabase.db"];
        success = [fileManager copyItemAtPath:defaultFilePath toPath:filePath error:&error];
    }
}

- (NSString *)getFile
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [paths objectAtIndex:0];
    return [documentsDir stringByAppendingPathComponent:@"BQWDatabase.db"];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [self copyFileIfNeeded];

    return YES;
}

但是Documents文件夹总是空的,我该如何解决?谢谢。

编辑:

enter image description here

2 个答案:

答案 0 :(得分:1)

您确定构建中包含BQWDatabase.db吗?您可以通过打开XCode中的右侧窗格进行检查,选择顶部最左侧的图标,然后展开“目标成员资格”。

答案 1 :(得分:0)

我在自己的代码中做了类似的事情,除了我找到默认文件如下:

NSString *defaultFilePath = [[[NSBundle mainBundle] pathForResource:@"BQWDatabase" ofType:@"db"];

不确定它是否有所作为

相关问题