ios 字典和数组有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9416053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between a dictionary and an array?
提问by Hyman Humphries
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!
字典和数组有什么区别,尤其是在处理 PLIST 文件时?使用一种比另一种有什么优势?谢谢!
回答by ikuramedia
Both NSDictionary
and NSArray
are collection classes, i.e. the group together other objects.
这两个NSDictionary
和NSArray
是集合类,即基团以及其他目的。
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.
NSArray 是一个“有序集合”——集合中的每个项目都有一个整数索引,因此项目有一个明确的顺序。如果您交换集合中项目的顺序,则集合不再“相同”,因为顺序不同。一个对象可能在集合中出现多次。
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.
NSSet 是一个“无序集合”——每个项目都出现在一个包中,顺序无关紧要,一个对象在包中只能存在一次。
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.
NSDictionary 是一个“索引集合”——集合中的每个项目都有一个键,可以用该键检索。一个对象可能出现多次,因为不同的键可能指向同一个对象,但一个键只能出现一次。如果您有计算机科学背景,字典也是“哈希表”的一种形式。
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.
在解析 PLIST 时,数组和字典是您处理的主要类型。当您在 Xcode 中编辑 PLIST 时 - 如果您将某些内容设置为 Array 类型,那么它的所有子项都被列为“Item 0, Item 1, Item 2 ...”,而如果您将其设置为 Dictionary 类型,则它是孩子是键:值对。
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.
想象一个包含许多文章的杂志应用程序。文章的顺序很重要,因此您可以将每篇文章存储在一个数组中。如果你想改变文章的顺序,你可以改变 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.
文章本身可能由词典表示,可能包含诸如“TextFile”、“Background”、“ArticleType”之类的键。您使用 Dictionary 是因为您可能会在将来的某个时候向该词典添加更多信息,而 key:value 机制使您的代码易于理解。
回答by JiaYow
An array is just a sorted list of objects. A dictionary stores key-value pairs.
数组只是对象的排序列表。字典存储键值对。
For example:
例如:
Array: obj1, obj2, ob3, ...
Dictionary:
字典:
{
@"Name": @"Bob"
@"Age": 20 (but NSDictionary can only store objects, so that would be a NSNumber)
}
There are no advantages or disadvantages, they are just two data structures, and you use the one you need.
没有优点或缺点,它们只是两种数据结构,您使用您需要的一种。
回答by Lorenzo B
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 object NSString
?
此外,使用字典,您可以以更用户友好的方式(助记符)使用特定键访问特定对象。例如,在字典中,您确定使用特定键(例如“text”)与特定对象(例如 )相关联NSString
。这对数组可能同样有效,但有一些困难。例如,您如何确定在索引 0 处有特定对象NSString
?
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.
关于您的问题,据我所知(自 Xcode 4 起),plist 将字典作为根对象。有关更多信息,请参阅how-do-you-change-a-plist-root-object-type-to-nsarray-in-xcode-4。
Hope it helps.
希望能帮助到你。
回答by hburde
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).
字典将键与值(对象)相关联,并且不保留项目的顺序。数组访问是通过索引完成的 - 顺序被保留。PLIst 添加使用 XML 定义数据(键值对)的能力。
回答by Jitendra Deore
NSDictionary is useful whwn u want to access a particular key value. NsArray is useful when u want to access a sequntial data.
NSDictionary 在您想访问特定键值时很有用。当你想访问一个序列数据时, NsArray 很有用。
回答by rakeshNS
NSDictionary does not retain its 'value' objects, but an NSMutableArray retain the objects added to it. For more information NSDictionaryand NSArray
NSDictionary 不保留其“值”对象,但 NSMutableArray 保留添加到其中的对象。更多信息NSDictionary和NSArray