我有一个文件 event_setup.sqlite,它放在我的应用程序中并复制到 Build Phases 中的 Copy Bundle Resources 部分。

当涉及到将此文件从资源复制到文档目录时,我的应用程序说找不到该文件。我正在使用以下代码从资源中复制文件并告诉我该文件不存在:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    [self checkAndCreateDatabase: @"event_setup.sqlite"];
    return YES;
}

-(void)checkAndCreateDatabase:(NSString *)name{
    /*----Get the path to the Database----*/
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:name];

    BOOL success;
    NSFileManager *fileManager = [NSFileManager defaultManager];

    success = [fileManager fileExistsAtPath:databasePath];

    if(success){
        NSLog(@"Database already exists");
        return;
    }
    else{
        NSLog(@"Database does not exist");
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:name];

        bool test = [fileManager fileExistsAtPath:databasePathFromApp];
        if(test){
            NSLog(@"file does exist in resources: %@", databasePathFromApp);
        }
        else{
            NSLog(@"file does not exist in resources: %@", databasePathFromApp);
        }

        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
    }
}

当我运行此代码时,我在日志中得到以下信息:
file does not exist in resources: /Users/samrowley/Library/Application Support/iPhone Simulator/6.1/Applications/583D4293-3AD5-45FB-B502-9919F51FDB7B/Test.app/event_setup.sqlite

我试过清理应用程序,重新启动 Xcode,删除文件并将其重新复制到 Xcode 中,但似乎没有任何效果。

编辑:

终端命令的输出:
-rw-r--r--   1 samrowley  staff    2797 11 Feb 10:51 divider.png
-rw-r--r--   1 samrowley  staff    2803 11 Feb 10:51 divider@2x.png
drwxr-xr-x   3 samrowley  staff     102 11 Feb 10:51 en.lproj
-rw-r--r--   1 samrowley  staff    2937 11 Feb 10:51 header.png
-rw-r--r--   1 samrowley  staff    3205 11 Feb 10:51 header@2x.png

最佳答案

这个问题的答案是文件没有正确添加到目标。要将其添加到目标,您需要进入文件检查器,在目标检查器下,必须检查该文件的应用程序名称。

关于iphone - SQLite 文件未添加到 bundle 资源中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14810908/

10-12 00:31
查看更多