NSDictionary与有序键 [英] NSDictionary with ordered keys

查看:105
本文介绍了NSDictionary与有序键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果这是任何其他人发现自己的情况。我有一个NSDictionary(存储在plist),我基本上使用作为关联数组(字符串作为键和值)。我想使用键盘数组作为我的应用程序的一部分,但我希望他们在一个特定的顺序(不是一个真正的顺序,我可以写一个算法来排序他们)。我总是可以存储一个单独的键的数组,但这似乎是一个kludgey,因为我总是必须更新字典的键以及数组的值,并确保它们总是对应。目前我只是使用[myDictionary allKeys],但显然这会返回一个任意的,无保证的顺序。在Objective-C中有一个数据结构,我错过了吗?

I'm curious if this is a situation any other folks have found themselves in. I have an NSDictionary (stored in a plist) that I'm basically using as an associative array (strings as keys and values). I want to use the array of keys as part of my application, but I'd like them to be in a specific order (not really an order that I can write an algorithm to sort them into). I could always store a separate array of the keys, but that seems kind of kludgey because I'd always have to update the keys of the dictionary as well as the values of the array, and make sure they always correspond. Currently I just use [myDictionary allKeys], but obviously this returns them in an arbitrary, non-guaranteed order. Is there a data structure in Objective-C that I'm missing? Does anyone have any suggestions on how to more elegantly do this?

推荐答案

Matt Gallagher刚刚写了一篇博文:OrderedDictionary:Subclassing一个Cocoa类集群完全覆盖这个问题,完整的示例代码。

Matt Gallagher just wrote a blog post titled "OrderedDictionary: Subclassing a Cocoa class cluster" covering exactly this issue, complete with sample code.

这篇关于NSDictionary与有序键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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