xcode 目标 c 何时使用 NSDictionary 而不是 NSArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14493725/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:36:38  来源:igfitidea点击:

objective c when to use NSDictionary instead of NSArray

iosobjective-cxcodensmutablearraynsmutabledictionary

提问by gdubs

I'm in a dilemma in terms of which of the two I should use. I will be retrieving a group of data via a restful API (returns json) and I'm not sure how I should store them before I display it on my UI View Table.

在我应该使用这两者中的哪一个方面,我处于两难境地。我将通过 Restful API 检索一组数据(返回 json),但在将它们显示在 UI 视图表上之前,我不确定应该如何存储它们。

eg.

例如。

{"Events":[{"Id":5,"Name":"Event 1 2013"},{"Id":6,"Name":"Event 2 2013"}]}

I've been reading tutorials and some would use NSMutableArrays while some would use NSMutableDictionary.

我一直在阅读教程,有些会使用 NSMutableArrays,而有些会使用 NSMutableDictionary。

How should I go about it?

我该怎么办?

BTW: I'm displaying the data on UI View table that will redirect the user to another page when tapped and if they decide to go back will have to show the previous view without reloading (uses UinavigationController)

顺便说一句:我在 UI 视图表上显示数据,当用户点击时会将用户重定向到另一个页面,如果他们决定返回,则必须在不重新加载的情况下显示上一个视图(使用 UinavigationController)

Thanks!

谢谢!

EDIT:

编辑:

Also, just to give you an idea on what I'm trying to do. I'm trying to follow this tutorial on splitting the data I get into section headers. On this tutorial it's using NSDictionary.

另外,只是为了让您了解我正在尝试做的事情。我正在尝试按照本教程将我获得的数据拆分为部分标题。在本教程中,它使用 NSDictionary。

http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

http://www.icodeblog.com/2010/12/10/implementing-uitableview-sections-from-an-nsarray-of-nsdictionary-objects/

If I use NSArray, would that affect the performance?

如果我使用 NSArray,会影响性能吗?

回答by Master Stroke

In NSArray - every item in the collection has an integer index, so there is an explicit order to the items. When you're retrieving/replacing/removing the stored object from the NSARRY,you need to specify the corresponding object index of that stored object.

在 NSArray 中 - 集合中的每个项目都有一个整数索引,因此项目有明确的顺序。当您从 NSARRY 中检索/替换/删除存储的对象时,您需要指定该存储对象的相应对象索引。

NSDictionary - derived from the word called entry. Each entry consists of one object that represents the key and a second object that is that key's value. Within a dictionary, the keys are unique. That is, no two keys in a single dictionary are equal (as determined by isEqual:).When you're retrieving the object from the dictionary you need to specify the key value for the objectForKeyWhenever if you're parsing the plist then NSDictionary would be ideal.You can refer apple's document herefor more explanation about NSDictionary.Happy coding :)

NSDictionary - 源自称为 entry 的词。每个条目由一个表示键的对象和另一个表示该键值的对象组成。在字典中,键是唯一的。也就是说,单个字典中没有两个键是相等的(由 isEqual: 确定)。当您从字典中检索对象时,您需要指定objectForKey 的键值,每当您解析 plist 然后 NSDictionary将是理想的。您可以在此处参考苹果的文档,以获取有关 NSDictionary.Happy 编码的更多解释:)

回答by jake

The lookup times on NSDictionaries are faster than on NSArrays. That's one of the main advantages. Here's a linkto the Apple documentation.

NSDictionaries 的查找时间比 NSArrays 快。这是主要优势之一。这是Apple 文档的链接

回答by lnafziger

Generally, if you need to access data in an indexed fashion (like you need to for rows in a table) then you should use an array because you can access any specific index using indexOfObject:

通常,如果您需要以索引方式访问数据(就像您需要访问表中的行一样),那么您应该使用数组,因为您可以使用 indexOfObject:

Now, if you have a lot of information for each row then you should have an array of either custom objects or an array of dictionaries.

现在,如果每一行都有很多信息,那么你应该有一个自定义对象数组或字典数组。

回答by Anoop Vaidya

Dictionary are always faster than Arrays. Dictionary maps keys to objects, just like a hash table. It's an associative array.

字典总是比数组快。字典将键映射到对象,就像哈希表一样。这是一个关联数组。

For searching some value you need to iterate for arrays, in dictionary you retrieve it by key.

为了搜索某些值,您需要迭代数组,在字典中通过键检索它。

If you want the collection to be in some sorted order or arrival order then Array is the proper type for you.

如果您希望集合按某种排序顺序或到达顺序排列,那么 Array 是适合您的类型。

Dictionary lacks when you end up getting two same keys.

当你最终得到两个相同的键时,字典就没有了。

And I feel good to use arrays for tableViews as I can directly associate row to index.

而且我觉得将数组用于 tableViews 感觉很好,因为我可以直接将行与索引相关联。