字典和数组之间有什么区别? [英] What's the difference between a dictionary and an array?

查看:824
本文介绍了字典和数组之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字典和数组之间有什么区别,尤其是在处理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!

推荐答案

两者 NSDictionary NSArray 是集合类,即将其他对象组合在一起。

Both NSDictionary and NSArray are collection classes, i.e. the group together other objects.

NSArray是'有序集合' - 集合中的每个项目都有一个整数index,因此对项目有明确的顺序。如果您交换集合中的项目顺序,则集合不再是与订单不同的相同。一个对象可能在集合中出现不止一次。

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.

NSSet是一个'无序集合' - 每个项目都出现在一个包中,顺序无关紧要对象只能在包中存在一次。

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.

NSDictionary是一个索引集合 - 集合中的每个项目都有一个密钥,可以使用该密钥检索。一个对象可能出现不止一次,因为不同的键可能指向同一个对象,但一个键只能出现一次。如果你有计算机科学背景,字典也是'哈希表'的一种形式。

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.

当解析PLIST时,数组和字典是你要处理的主要类型。当您在Xcode中编辑PLIST时 - 如果您将某些内容设置为数组类型,那么它的所有子项都会列为项目0,项目1,项目2 ......,而如果将其设置为字典类型,那么它就是儿童是关键:价值对。

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.

想象一下杂志申请,其中包含许多文章。文章的顺序很重要,因此您可以将每篇文章存储在一个数组中。如果你想改变文章的顺序,你会改变plist中数组的顺序。

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.

文章本身可能由字典表示,也许包含键例如TextFile,Background,ArticleType。您使用字典是因为您可以在将来的某个时刻向字典添加更多信息,而key:value机制使您的代码可以理解。

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.

这篇关于字典和数组之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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