如何将文件添加到 XCode 中的资源文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5025826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 20:15:01  来源:igfitidea点击:

How do I add files to the resources folder in XCode?

iphonexcodesqlitexcode4

提问by Gazzer

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 添加一个 sqlite 数据库(也适用于 XCode 3)。教程说明将 .db 文件添加到资源文件夹,我想这会在构建过程中被复制到~/Library/Application Support/iPhone Simulator/4.2/Applications/{some-id}/Documents/在那里你可以找到文件NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)等等。

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

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

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!)

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

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

(由于数据的性质,我在 CoreData 上坚持使用 sqlite)

回答by meronix

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:

你必须开始在你的项目 xcode 中添加你的数据库,所以它会被添加到你的包文件夹中,你可以在那里通过代码找到它:

[NSBundle mainBundle]

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:

它是您在“构建”应用程序时可以通过 xcode 添加文件的唯一文件夹(最终使用子文件夹,而不是“系统”文件夹作为“文档”),现在您只需要记住主包文件夹只是“只读”,因此您不能在那里使用具有写入权限的数据库。所以正常的方法是:

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

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

2) copy it from the main bundle

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) use it (r/w) in the documents folder

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

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...

ps:并且(当然)请记住,当您在 iPhone/模拟器中使用/编辑/编写数据库时,可能会添加记录,主包中的记录以及 mac/项目中的记录不会更新,不会向其中添加任何记录,因此如果出于任何原因您在 iPhone/模拟器上删除您的应用程序(或通过 xCode“构建”菜单“清除所有目标”),检查/复制方法将复制“原始”数据库再次打开文档文件夹,因此您将丢失所有更改...

回答by StuR

Be careful with the amount of data you are putting into the Documents folder, this is meant for user data and since this data will be backed up using iCloud Apple have limited the amount of data an app can store and use in the Documents folder.

请注意您放入 Documents 文件夹的数据量,这是用于用户数据的,并且由于这些数据将使用 iCloud 进行备份,Apple 限制了应用程序可以在 Documents 文件夹中存储和使用的数据量。

My app was rejected for using a 6MB SQLite database in this way. Instead copy it to the caches directory: NSCachesDirectory.

我的应用程序因以这种方式使用 6MB SQLite 数据库而被拒绝。而是将其复制到缓存目录:NSCachesDirectory。

Or prevent the file from being backed up: https://developer.apple.com/library/ios/#qa/qa1719/_index.html

或者防止文件被备份:https: //developer.apple.com/library/ios/#qa/qa1719/_index.html