如何在 iPhone sdk 中序列化一个简单的对象?

发布于 07-22 05:49 字数 382 浏览 4 评论 0原文

我有一本对象字典; 它们都是应该可序列化的 POCO 对象。 我应该采用什么技术将它们写入磁盘。 我正在寻找最简单的选项来编写一些列表来保存状态。

我想我有3个选择。

  1. plist 文件。 然而,这似乎仅限于存储预定义的对象(字符串、数字等),而不是对象(如具有姓名和年龄的人)。

  2. 核心数据。 (3.0 中的新增功能)这会很好用; 但是我的数据模型需要改变才能使这项工作起作用。 这将是一次大规模的返工,我不确定是否值得付出努力。

  3. SQLLite。 实现一个简单的 SQL 数据库来读取和读取。 我对此进行了最少的研究,但我不想“重写”一些核心数据 ORM 函数。

I have a dictionary of objects; they are all POCO objects that should be serializable. What technique should I look at for writing these to disk. I'm looking for the simplest option to write a few lists to save state.

I think I have 3 options.

  1. plist files. However this seems to be limited to only storing predefined objects (strings, numbers etc) not objects (like a person with a name and age).

  2. CoreData. (New in 3.0) This would work well; however my data model would need to change to make this work. This would be a massive rework and I'm not sure if it is worth the effort.

  3. SQLLite. Implement a simple SQL database to read to and from. I have done the least amount of reserch into this one, but I don't want to have to 'rewrite' some of the core data ORM functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁拥2024-07-29 05:49:19

要序列化自定义对象,您只需遵守 NSCoding 协议。 如果您的对象扩展了 NSObject,您所需要做的就是(我相信)实现这些(例如 person 对象):

// Encode an object for an archive
- (void)encodeWithCoder:(NSCoder *)coder
{
    [super encodeWithCoder:coder];
    [coder encodeObject:name forKey:@“Name”];
    [coder encodeInteger:age forKey:@“Age”];
}
// Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    name = [[coder decodeObjectForKey:@“Name”] retain];
    age  = [coder decodeIntegerForKey:@“Age”];
}

NSArrayNSDictionary 已经实现了序列化方法。 它们将序列化它们所持有的所有对象(如果对象实现 NSCoder 接口 - 如果它们扩展 NSObject,则它们会序列化)。 NSObject 的 encodeWithCoderinitWithCoder 默认情况下不执行任何操作,因此除非您在类中实现自己的代码,否则不会序列化任何内容。

如果你有 NSArray 或 NSDictionary 对象,你可以使用以下方法同步它们:

// Writing
- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag;
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;
// Reading
- (id)initWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfURL:(NSURL *)aURL;

To serialize custom object you just need to conform to the NSCoding protocol. If your object extends NSObject all you need to do (I believe) is to implement these (example for person object):

// Encode an object for an archive
- (void)encodeWithCoder:(NSCoder *)coder
{
    [super encodeWithCoder:coder];
    [coder encodeObject:name forKey:@“Name”];
    [coder encodeInteger:age forKey:@“Age”];
}
// Decode an object from an archive
- (id)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    name = [[coder decodeObjectForKey:@“Name”] retain];
    age  = [coder decodeIntegerForKey:@“Age”];
}

NSArray and NSDictionary already implement methods for serialization. They will serialize all the objects that they hold (if objects implement NSCoder interface - they do if they extend NSObject). NSObject's encodeWithCoder and initWithCoder do nothing by default so unless you implement your own code in your classes nothing gets serialized.

If you have NSArray or NSDictionary of objects you can synchronize them using:

// Writing
- (BOOL)writeToFile:(NSString *)aPath atomically:(BOOL)flag;
- (BOOL)writeToURL:(NSURL *)aURL atomically:(BOOL)flag;
// Reading
- (id)initWithContentsOfFile:(NSString *)aPath;
- (id)initWithContentsOfURL:(NSURL *)aURL;
时间你老了2024-07-29 05:49:19

您执行此操作的方式与在 Mac OS X 上执行的方式相同:您的 POCO 必须符合 NSCoding 协议。 请参阅此处获取概念参考,以及此处 NSCoding 参考。

如果数据不是那么广泛,并且对象之间没有极其复杂的关系,那么将所有内容写成 plist 可能是您的最佳选择; 它的执行速度非常快,并且在代码中实现起来很简单。 正如您所说,CoreData 可能需要大量的额外工作来适应您的代码,而 sqlite 确实只适用于存储非常适合存储在关系数据库中的数据。 请记住,与使用二进制 plist 相比,sqlite 也更慢并且使用更多资源。

You do this in the same way you'd do it on Mac OS X: your POCOs must conform to the NSCoding protocol. See here for a conceptual reference, and here for the NSCoding reference.

If the data isn't that crazy extensive and you don't have ridiculously complicated relationships between your objects, writing everything out as a plist is probably your best option; it's very fast to execute and simple to implement in your code. Like you said, CoreData will probably be a lot of extra work to adapt your code to, and sqlite really is only good for storing data that's perfect to be stored in a relational database. Keep in mind that sqlite is also slower and uses more resources than working with binary plists.

征棹2024-07-29 05:49:19

NSCoder 是标准的 Cocoa序列化的习语。

NSCoder is the standard Cocoa idiom for serialization.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文