iOS:如何保存在应用程序中创建的自定义对象 [英] iOS: How to save a custom object that is created in an app

查看:136
本文介绍了iOS:如何保存在应用程序中创建的自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读取保存对象,保存状态等,但我还是有点困惑的路线,我应该采取当保存从我的应用程序创建的对象。我已经建立了一个应用程序,用户可以创建一个类型,名称和频率的无线电台。一旦用户点击保存,将使用自定义类创建RadioStation对象,并存储在放置在RootViewController中的NSMutableArray中。虽然RadioStation对象可以存储在此数组中并在表视图中正确显示,但很显然,一旦应用程序退出,这些对象就不再被应用程序占有。

I've been reading up on saving objects, preserving state etc, but I'm still a little confused on the route I should take when saving objects that are created from my app. I have built an app where a user can create a Radio Station with the Genre, name and frequency. Once a user hits save, a "RadioStation" object is created using a custom class and is stored in an NSMutableArray that is placed in the RootViewController. While RadioStation objects can be stored in this array and are displayed correctly in the table view, it's obvious that once the application quits, these are no longer going to be held by the application.

在这一点上,我不知道我应该如何开始构建这个设计。我需要设置plist吗?我应该使用核心数据与SQLite DB吗?我需要研究iOS对象序列化这两种情况吗?

At this point I'm not sure how I should begin to architect this design. Do I need to set up a plist? Should I use Core Data with a SQLite DB? Do I need to research iOS object serialization for both these scenarios? Or is there an more straight forward way that I'm not aware of?

推荐答案

最强大的方法是让你的自定义对象是 NSManagedObject 的子类,并使用Core Data将其存储在 NSManagedObjectContext 中。在iOS上,这将始终使用sqlite数据库,在Mac OS X上,您可以使用sqlite,xml或专有的二进制格式(比xml更小/更快)。

The most powerful approach is to make your custom object a subclass of NSManagedObject, and store it in an NSManagedObjectContext using Core Data. On iOS this will always use an sqlite database, on Mac OS X you can use sqlite, xml, or a proprietary binary format (smaller/faster than xml).

你的数据结构是多么复杂,以及它在未来可能会改变,这种技术可能或可能不太复杂的应用程序。这是很多工作要设置,并且API是非常复杂,但它是最好的选择,如果你需要存储成千上万/数百万的对象在一个文件。

Depending on how complicated your data structure is, and how likely it is to change in future, this technique may or may not be too complicated for your app. It's a lot of work to setup, and the API is very complicated, but it's the best choice if you need to store tens of thousands/millions of objects in a file.

对于你的应用程序,我会有一个单一的sqlite文件包含应用程序中的所有广播电台。

For your app, I would have a single sqlite file containing all of the radio stations in the app.

选项,是在自定义对象的类中实现 NSCoding 协议。在此处记录:

The simplest option, is to implement the NSCoding protocol in your custom object's class. Documented here:

http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Archiving/Articles/codingobjects.html#//apple_ref/doc/uid/ 20000948-BCIHBJDE

基本理论是将对象写入或读取所有数据到编码器,然后将编码器传递到任何子对象(s)。

The basic theory is your object writes or reads all it's data to a "coder", and then passes the coder on to any child object(s). Outside of the class, you use an "archiver" or an "unarchiver" class to coordinate everything.

一旦你的自定义类实现了 NSCoding

Once your custom class implements NSCoding you can read/write it to the disk with:

myObject = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
[NSKeyedArchiver archiveRootObject:myObject toFile:filePath];

这是一个非常简单的方法,但它有一些缺点,例如你需要以自己的方式支持不同版本的文件格式(例如可能使用不同的文件扩展名)。

This is a very simple approach, but it has a few drawbacks, for example you will need to figure out your own way to support different versions of the file format (perhaps with a different file extension for example).

Cocoa / CocoaTouch中的许多类已经实现了 NSCoding ,因此它们可以以这种方式写入文件(包括 NSArray NSString NSNumber 等)。

Many classes in Cocoa/CocoaTouch already implement NSCoding, so they can be written to a file in this fashion (including NSArray, NSString, NSNumber, etc).

对于你的应用程序,我会用它来存储每个广播电台在单个文件中。应用程式启动时,从目录载入所有电台。您应该考虑为您的应用启用iTunes拖放文件共享,因此用户可以轻松地与朋友或设备之间共享电台文件。

For your app, i would use this to store each radio station in a single file. When the app launches, load all the radio stations from a directory. You should consider enabling iTunes drag/drop filesharing for your app, so users can easily share radio station files with their friends or between devices.

这也适合与iCloud在设备之间同步,我没有看过iCloud很多,但我期望每个广播电台一个文件将是实现iCloud同步的理想方式。

This would also fit in well with iCloud for syncing between devices, I haven't looked into iCloud much, but I expect one file per radio station will be the ideal way to implement iCloud syncing.

这篇关于iOS:如何保存在应用程序中创建的自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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