如何将文件添加到XCode中的资源文件夹? [英] How do I add files to the resources folder in XCode?

查看:194
本文介绍了如何将文件添加到XCode中的资源文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个sqlite数据库添加到XCode 4(也适用于XCode 3)。教程说明将.db文件添加到资源文件夹,我想这会在构建期间复制到〜/ Library / Application Support / iPhone Simulator / 4.2 / Applications / {some-id} / Documents / 你可以在哪里找到文件 NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)
等。

I want to add a sqlite database to XCode 4 (applies to XCode 3 too). Tutorials state adding the .db file to the resources folder, and I suppose this gets copied to ~/Library/Application Support/iPhone Simulator/4.2/Applications/{some-id}/Documents/ during build where you can find the file with NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) etc.

但是,XCode 4没有可见资源文件夹。

However, XCode 4 doesn't have a visible resources folder.

我尝试使用Add File ...命令添加文件,然后它出现在Targets> AppName> Copy Bundle Resources,但总是一个空的.db文件出现在上面的文档文件夹中(然后我手动替换 - 显然不是正确的方法!)

I've tried adding a file with the Add File... command, and then it appears in Targets > AppName > Copy Bundle Resources, but always an empty .db file appears in the above documents folder (which I then manually replace - obviously not the correct approach!)

(到期我坚持使用sqlite而不是CoreData的数据的性质

(due to the nature of the data I'm sticking with sqlite over CoreData)

推荐答案

你必须开始在你的项目中添加你的数据库-xcode,因此它将被添加到您的bundle文件夹中,您可以通过代码找到它:

you have to start adding your db in your project-xcode, so it will be added in your bundle folder, where you can find it via code:

[NSBundle mainBundle]

这是你构建应用程序时可以通过xcode添加文件的唯一文件夹(最终使用子文件夹,但不是系统文件夹作为文档)现在你只需要记住主bundle文件夹只是只读,所以你不能使用你的db具有写权限。
所以通常的方法是:

It's the only folder where you can add files via xcode when you "build" your app (eventually with subfolders, but not "system" folders as "documents") now you just need to keep in mind that the main bundle folder is just "read only", so you cant use your db there with write privileges. So the normal way is:

1)当你想使用你的数据库时,检查代码是否存在于app:documents文件夹中。
当然第一次没有,所以

1) when you wanna use your db, check via code if it exists in the app:documents folder. Of course the first time it doesn't, so

2)从主包中复制

- (BOOL)transferDb {
    NSError **error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourData.db"]; 

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path])
    {
        NSString *bundle =  [[ NSBundle mainBundle] pathForResource:@"preferenze" ofType:@"plist"];
        [fileManager copyItemAtPath:bundle toPath:path error:error];
        return YES;
    }
    return NO;
}

3)在文件夹中使用它(r / w)

3) use it (r/w) in the documents folder

ps:
和(当然)请记住,当您在iPhone /模拟器中使用/编辑/编写数据库时,可能会添加记录,主要是捆绑,当然你的mac / project中的那个不会被更新,也不会添加任何记录,所以如果你出于任何原因在iPhone /模拟器上删除你的应用程序(或者通过xCodebuild清理所有目标) 菜单检查/复制方法将再次复制文档文件夹中的virgin数据库,因此您将丢失所有更改...

ps: and (of course) keep in mind that when you use/edit/write the db in iPhone/simulator, maybe adding records, the one in the main bundle and of course the one in your mac/project won't be updated, no records will be added to it, so if for any reason you delete your app on iPhone/simulator (or "clean all targets" by the xCode "build" menu) the check/copy method will copy the "virgin" db in the documents folder again, so you will loose all your changes...

这篇关于如何将文件添加到XCode中的资源文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆