字典和数组有什么区别?
字典和数组有什么区别,特别是在使用 PLIST 文件时?使用其中一种相对于另一种有什么优点?谢谢!
What is the difference between a dictionary and an array, especially when working with PLIST files? What are the advantages of using one over the other? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
NSDictionary
和NSArray
都是集合类,即将其他对象组合在一起。NSArray 是一个“有序集合”——集合中的每个项目都有一个整数索引,因此项目有明确的顺序。如果交换集合中项目的顺序,则集合将不再“相同”,因为顺序不同。一个对象可能在集合中出现多次。
NSSet 是一个“无序集合”——每个项目都出现在一个包中,顺序并不重要,并且一个对象只能在包中存在一次。
NSDictionary 是一个“索引集合”——集合中的每个项目都有一个键,并且可以使用该键进行检索。一个对象可能会出现多次,因为不同的键可能指向同一个对象,但一个键只能出现一次。如果您有计算机科学背景,字典也是“哈希表”的一种形式。
解析 PLIST 时,数组和字典是您处理的主要类型。当您在 Xcode 中编辑 PLIST 时 - 如果您将某些内容设置为数组类型,则它的所有子项都会列为“Item 0、Item 1、Item 2...”,而如果您将其设置为 Dictionary 类型,则它是孩子是键:值对。
不同类型的一个重要用例如下。
想象一个包含许多文章的杂志应用程序。文章的顺序很重要,因此您可以将每篇文章存储在一个数组中。如果您想更改文章的顺序,您可以更改 plist 中数组的顺序。
文章本身可以由字典表示,可能包含诸如“TextFile”、“Background”、“ArticleType”之类的键。您使用字典是因为您可能在将来的某个时候向字典添加更多信息,并且 key:value 机制使您的代码易于理解。
Both
NSDictionary
andNSArray
are collection classes, i.e. the group together other objects.An NSArray is an 'ordered collection' - every item in the collection has an integer index, so there is an explicit order to the items. If you swap the order of items in the collection then the collection is no longer the 'same' as the order is different. An object may appear more than once in the collection.
An NSSet is an 'unordered collection' - every item appears in a bag, the order doesn't matter and an object can only exist once in the bag.
An NSDictionary is an 'indexed collection' - every item in the collection has a key and can be retrieved with that key. An object may appear more than once, in that different keys may point to the same object, but a key can only appear once. A dictionary is also a form of 'hash table' if you have a computer science background.
When parsing PLISTs, Arrays and Dictionaries are the main types you deal with. When you edit a PLIST in Xcode - if you set something as an Array type, then all of it's children are listed as "Item 0, Item 1, Item 2..." whereas if you set it as a Dictionary type, then it's children are key:value pairs.
One significant use case for the difference types is as follows.
Imagine a magazine application which contains a number of articles. The order of the articles is important, and so you would store each article in an array. If you wanted to change the order of the articles, you would change the order of the array in the plist.
The articles themselves may be represented by Dictionaries, perhaps containing keys such as "TextFile", "Background", "ArticleType". You use a Dictionary because you may add more information to the dictionary at some point in the future, and the key:value mechanism makes your code understandable.
数组只是对象的排序列表。字典存储键值对。
例如:
字典:
没有什么优缺点,只是两种数据结构,你需要的就用那个。
An array is just a sorted list of objects. A dictionary stores key-value pairs.
For example:
Dictionary:
There are no advantages or disadvantages, they are just two data structures, and you use the one you need.
关键的区别在于您如何访问它们。
数组和字典都是容器,并且可以按顺序读取(例如,数组可以通过索引来枚举,字典可以通过键来枚举)。但是,数组维持对象之间的顺序,而字典则不然。
此外,使用字典,您可以以一种更用户友好的方式(助记符)使用特定键访问特定对象。例如,在字典中,您可以确定与特定键(例如“文本”)相关联的特定对象(例如
NSString
)。这同样适用于数组,但有一些困难。例如,您如何确定索引 0 处存在特定对象NSString
?关于你的问题,据我所知(自 Xcode 4 起),plists 有一个字典作为根对象。有关更多信息,请参阅 如何将 a-plists-root-object-type-to-nsarray-in-xcode-4 更改为 nsarray-in-xcode-4。
希望有帮助。
The key difference is how you can access within them.
Both arrays and dictionaries are containers and can be read sequentally (e.g. arrays can be enumerated by means of an index and dictionaries by means of a key). But while arrays maintain the order amongs objects, dictionaries not.
In addition, with dictionaries you have the possibility to access a specific object with a specific key in a more user-friendly way (a mnemonic one). For example in a dictionary you know for sure that with a specific key, say "text", is associated a specific object, say
NSString
. The same could be valid for an array but with some difficulties. For example, how are you sure that at index 0 there is the specific objectNSString
?About your question and as far I know (since Xcode 4), plists have as the root object a dictionary. For further info see how-do-you-change-a-plists-root-object-type-to-nsarray-in-xcode-4.
Hope it helps.
字典将键与值(对象)相关联,并且不保留项目的顺序。数组访问是通过索引完成的 - 顺序被保留。 PLIt 添加使用 XML 定义数据(键、值对)的能力。
Dictionaries associate a key with a value(object) and don't preserve the order of items. Array access is done by index - the order is preserved. PLIst add the ability use use XML for defining the data (key,value pairs).
当你想要访问特定的键值时,NSDictionary 很有用。
当您想要访问顺序数据时,NsArray 非常有用。
NSDictionary is useful whwn u want to access a particular key value.
NsArray is useful when u want to access a sequntial data.
NSDictionary 不保留其“值”对象,但 NSMutableArray 保留添加到其中的对象。更多信息NSDictionary和<一个href="https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html" rel="nofollow">NSArray
NSDictionary does not retain its 'value' objects, but an NSMutableArray retain the objects added to it. For more information NSDictionary and NSArray