持久自定义对象 [英] Persisting Custom Objects

查看:106
本文介绍了持久自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义对象,只是继承自NSObject。它有3个成员 - 两个浮点数和一个 NSDate

I have a custom object that simply inherits from NSObject. It has 3 members - two floats and an NSDate.

我的应用程序将有一个带数字的数组这些物体踢来跑去,我需要在跑步之间坚持下去。我怎么能做到这一点呢?

My app is going to have an array with a number of these objects kicking around, and I need to persist it between runs. How can I accomplish this?

我考虑过使用SQLite数据库,但我认为这是因为我做过的唯一一次查询有点矫枉过正将选择*

I've thought about using a SQLite db, but I'm thinking that it's a bit overkill since the only query I'd ever do would be select *.

在理想的世界中,我想使用xml plist文件。我不确定我是否可以使用自定义对象执行此操作。我知道有一组属性列表对象, NSArray 属于这个,但是 writeToFile:atomically:只能工作与财产清单对象。

In an ideal world I'd like to use an xml plist file. I'm not sure if I can do this with my custom object though. I know there's a set of Property List objects, and that NSArray comes under that, but writeToFile:atomically: only works with property list objects.

任何想法都将不胜感激,谢谢!

Any thoughts would be appreciated, thanks!

推荐答案

NSCoding 将完全按照您的意愿行事。我建议您在Apple文档中阅读它,但我认为它非常简单易用。您的类(以及任何子类)需要实现 NSCoding 协议,您需要添加 -encodeWithCoder: -initWithCoder:对象的方法。大多数常见框架类已实现 NSCoding

NSCoding will do exactly what you want. I recommend you read up on it in the Apple docs, but I thought it was pretty straightforward to use. Your class (and any child classes) will need to implement the NSCoding protocol and you'll need to add -encodeWithCoder: and -initWithCoder: methods to your objects. Most of the common framework classes implement NSCoding already.

您的类的代码如下所示:

The code for your class will look something like this:

-(void) encodeWithCoder: (NSCoder*) coder {
  [coder encodeInteger: versionValue forKey: versionKey];
  [coder encodeObject: myStuff forKey: myStuffKey];
}

-(id) initWithCoder: (NSCoder*) coder {
  self = [super init];
  if ( ! self) return nil;
  myStuff = [[coder decodeObjectForKey: myStuffKey] retain];
  return self;
}

建议您在编码时添加版本号,以便灵活地管理更改在将来的版本中使用您的存档格式。

It's recommended you add a version number when encoding to give you flexibility to manage changes to your archive format in future versions.

在我的课堂上,我添加了一个方便的方法来存档我的对象:

In my class, I added a convenience method to archive my object:

-(void) archiveToFile: (NSString*) path {
  NSMutableData *data = [[NSMutableData alloc] init];
  NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
  [archiver encodeObject: self forKey: myArchiveKey];
  [archiver finishEncoding];
  [archiver release];
  [data writeToFile: path atomically: YES];
  [data release];
}

和另一个取消归档或创建新对象:

and another one to unarchive or create a new object:

+(MyArchive*) newFromFile: (NSString*) path
            orWithMyStuff: (MyStuff*) myStuff
{
  NSData *data = [[NSData alloc] initWithContentsOfFile: path];
  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
  MyArchive *myArchive = [unarchiver decodeObjectForKey: myArchiveKey];
  [unarchiver finishDecoding];

  if (myArchive) {
    [myArchive retain];
  } else {
    myArchive = [[MyArchive alloc] initWithStuff: myStuff;
  }
  [unarchiver release];
  [data release];
  return myArchive;
}

由于您的顶级对象是 NSArray ,你需要为你的情况修改最后两种方法,但大多数样板代码都是相同的。

Since your top level object is an NSArray, you'll need to modify the last two methods for your case, but most of the boilerplate code will be the same.

这篇关于持久自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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