目标 c 何时使用 NSDictionary 而不是 NSArray [英] objective c when to use NSDictionary instead of NSArray

查看:36
本文介绍了目标 c 何时使用 NSDictionary 而不是 NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如

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

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

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

我应该怎么做?

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

谢谢!

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

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.

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

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

If I use NSArray, would that affect the performance?

推荐答案

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

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.

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

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 objectForKey Whenever if you're parsing the plist then NSDictionary would be ideal.You can refer apple's document here for more explanation about NSDictionary.Happy coding :)

这篇关于目标 c 何时使用 NSDictionary 而不是 NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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