NSCoder VS NSDictionary,你什么时候用什么? [英] NSCoder vs NSDictionary, when do you use what?

查看:111
本文介绍了NSCoder VS NSDictionary,你什么时候用什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何决定何时使用NSDictionary或NSCoder / NSCoding?



看起来对于一般的属性列表和NSDictionary



当处理包含数据或其他自定义类的自定义类嵌套在内部时,它看起来像是一个简单的方法来生成XML文件,像NSCoder / NSCoding将是更好的路由,因为它将遍历所有包含的对象类,并在使用归档命令时对它们进行编码。



NSDictionary似乎需要更多的工作来获得所有的属性或数据特性到一个单一的水平才能保存它,NSCoder / NSCoding会自动编码



除了它是二进制数据,并且不能在你的应用程序之外编辑,是否有真正的理由使用一个在另一个?沿着这些线有一个指标,你应该在两者之间倾斜?我缺少一些明显的东西。

解决方案

Apple的文档对象图可以这样说:


Mac OS X序列化存储值对象的简单层次结构,例如字典,数组,字符串和二进制数据。序列化仅保留对象及其在层次结构中的位置的值。对反序列化时对同一值对象的多次引用可能会导致多个对象。



Mac OS X档案库存储任意复杂的对象图形。归档保留图中每个对象的身份以及它与图中所有其他对象的所有关系。当取消存档时,重建对象图应该有很少的例外,是原始对象图的精确副本。


这是,如果你想存储简单的,序列化(例如使用NSDictionary)是一个很好的方法去。如果要存储任意类型的对象图形,保留唯一性和可变性,使用存档(例如使用NSCoder)是最好的选择。



您可能还想阅读Apple的 Cocoa的编写指南和编程指南,其中对象图上的上述链接页是一部分,因为它很好地涵盖了这个主题。


I'm trying to figure out how to decide when to use NSDictionary or NSCoder/NSCoding?

It seems that for general property lists and such that NSDictionary is the easy way to go that generates XML files that are easily editable outside of the application.

When dealing with custom classes that holds data or possibly other custom classes nested inside, it seems like NSCoder/NSCoding would be the better route since it will step through all the contained object classes and encode them as well when an archive command is used.

NSDictionary seems like it would take more work to get all the properties or data characteristics to a single level to be able to save it, where as NSCoder/NSCoding would automatically encode nested custom classes that implement the NSCoding interface.

Outside of it being binary data and not editable outside of your application is there a real reason to use one over the other? And along those lines is there an indicator of which way you should lean between the two? Am I missing something obvious?

解决方案

Apple's documentation on object graphs has this to say:

Mac OS X serializations store a simple hierarchy of value objects, such as dictionaries, arrays, strings, and binary data. The serialization only preserves the values of the objects and their position in the hierarchy. Multiple references to the same value object might result in multiple objects when deserialized. The mutability of the objects is not maintained.

Mac OS X archives store an arbitrarily complex object graph. The archive preserves the identity of every object in the graph and all the relationships it has with all the other objects in the graph. When unarchived, the rebuilt object graph should, with few exceptions, be an exact copy of the original object graph.

The way I interpret this is that, if you want to store simple values, serialization (using an NSDictionary, for example) is a fine way to go. If you want to store an object graph of arbitrary types, with uniqueness and mutability preserved, using archives (with NSCoder, for example) is your best bet.

You may also want to read Apple's Archives and Serializations Programming Guide for Cocoa, of which the aforelinked page on object graphs is a part, as it covers this topic well.

这篇关于NSCoder VS NSDictionary,你什么时候用什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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