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

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

问题描述

我在做一个iPhone应用程序。在这个应用程序中,我只想有一个数据库,用作我的应用程序中的值的查找表。数据库只需要提供我需要的值,这取决于程序的查询。它不会在数据库中执行任何添加或删除。我的问题是如何使用CoreData初始化应用程序上的默认数据。所以,当程序启动它已经有所有需要的值。



如果你有什么更好的做什么或什么是其他选择的其他建议,请告诉我。 / p>

感谢。

解决方案

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



这将创建一个完全功能的界面,用于添加,删除或编辑该实体类型的属性。所有的东西都应该通过Cocoa Bindings挂钩,这样你就不需要写一行代码了。您可以以这种方式为模型中的每个实体类型添加接口。



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


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.

Thanks.

解决方案

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.

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.

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天全站免登陆