如何在 CoreData 应用程序中使用默认数据初始化存储? [英] How do I initialize a store with default data in a CoreData application?

查看:15
本文介绍了如何在 CoreData 应用程序中使用默认数据初始化存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个 iPhone 应用程序.在这个应用程序中,我只想有一个数据库用作我的应用程序中值的查找表.数据库唯一要做的就是根据程序的查询为我提供我需要的值.它不会在数据库中进行任何添加或删除操作.我的问题是如何使用 CoreData 在应用程序上初始化默认数据.这样当程序启动时它已经拥有了所有需要的值.

I'm doing an iPhone application. In this app, I just want to have a database to be used as a looked up table for values in my app. The only thing the database will do was to supply me the values I needed depending on the query of the program. It won't do any addition or deletion in the database. My question was how do I initialize a default data on the application using CoreData. So that when the program starts It already had all the values needed.

如果您对更好的做法或其他替代方案有其他建议,请告诉我.

If you have other suggestions of what is better do or what are other alternatives, please tell me.

谢谢.

推荐答案

我将创建一个简单的 Mac 应用程序,从基于 Core Data 文档的应用程序的模板开始.将您现有的 .xcdatamodel 复制到项目中的默认模型上(或添加新的数据模型,并确保在引用默认模型的任何地方重命名).在 Interface Builder 中打开文档窗口的 XIB 文件,然后从 Interface Builder 库检查器中将 Core Data Entity 项拖到其中.从出现的对话框中,选择要显示的实体并选择要在其中显示的界面.

I would create a simple Mac application, starting from the template for a Core Data document-based application. Copy your existing .xcdatamodel over the default one in the project (or add the new data model and be sure to rename anywhere that refers to the default model). Open up the XIB file for the document window in Interface Builder and drag the Core Data Entity item into it from the Interface Builder library inspector. From the resulting dialog, choose an entity to display and select an interface to display it in.

这将创建一个功能齐全的界面,用于添加、删除或编辑该实体类型的属性.一切都应该通过 Cocoa Bindings 连接起来,这样你就不需要编写一行代码来让它工作.您可以通过这种方式为模型中的每个实体类型添加接口.

What this will do is create a fully functional interface for adding, removing, or editing the properties of that entity type. Everything should be hooked up via Cocoa Bindings so that you don't need to write a line of code for this to work. You can add interfaces for each entity type in your model this way.

这将使您能够在 Core Data 文档中快速输入和编辑数据,然后您可以将其保存到磁盘并作为资源添加到您的 iPhone 应用程序中.SQLite 数据库结构在桌面和 iPhone Core Data 实现之间完全兼容,所以我发现这是测试 iPhone Core Data 代码的一种快速而简单的方法.

This will let you quickly enter and edit data within a Core Data document, which you can then save out to disk and add as a resource to your iPhone application. The SQLite database structures are fully compatible between the desktop and iPhone Core Data implementations, so I've found that this is a quick and easy way of testing your iPhone Core Data code.

这篇关于如何在 CoreData 应用程序中使用默认数据初始化存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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