我如何获得 NSDictionary/NSMutableDictionary 的原始顺序?

发布于 2024-09-12 14:44:11 字数 146 浏览 7 评论 0原文

我已经创建了带有 10 个键的 NSMutableDictionary。现在我想按照添加到 NSMutableDictionary 的顺序访问 NSMutableDictionary 键(使用 SetValue:* forKey:* );

我怎样才能做到这一点?

i have created NSMutableDictionary with 10 keys.Now i want to access NSMutableDictionary keys in a same order as it was added to NSMutableDictionary (using SetValue:* forKey:* );

How can i achieve that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

神回复 2024-09-19 14:44:11

如果您绝对必须使用字典容器,则必须使用可按添加键值对的顺序排序的键。因此,在创建字典时,您使用的键是自动递增整数或类似的键。然后,您可以对(整数)键进行排序并检索与这些键关联的值。

但是,如果您执行了所有这些操作,您也可以只使用 NSMutableArray 并直接向数组添加值!它将更快并且需要更少的代码。您只需按顺序检索对象:

for (id obj in myArray) { /* do stuff with obj... */ }

If you absolutely must use a dictionary container, you have to use a key that is sortable by the order in which you add key-value pairs. Thus, when creating your dictionary, you use a key that is an auto-incrementing integer or similar. You can then sort on the (integer) keys and retrieve the values associated with those keys.

If you do all of that, however, you may as well just use an NSMutableArray and add values to the array directly! It will be much faster and require less code. You just retrieve objects in order:

for (id obj in myArray) { /* do stuff with obj... */ }
一影成城 2024-09-19 14:44:11

NSMutableDictionary 无法做到这一点。看看例如 Matt Gallaghers OrderedDictionary

NSMutableDictionary can't do that. Take a look at e.g. Matt Gallaghers OrderedDictionary.

三月梨花 2024-09-19 14:44:11

我编写了一个快速方法来获取源数组(全部无序的对象)和引用数组(具有所需(且完全任意)顺序的对象),并返回一个数组,其中源数组的项目已重新组织以匹配参考数组。

- (NSArray *) reorderArray:(NSArray *)sourceArray toArray:(NSArray *)referenceArray
{
    NSMutableArray *returnArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < [referenceArray count]; i++)
    {
        if ([sourceArray containsObject:[referenceArray objectAtIndex:i]])
        {
            [returnArray addObject:[arrReference objectAtIndex:i]];
        }
    }
    return [returnArray copy];
}

请注意,这是非常脆弱的。它使用 NSArraycontainsObject: 方法,该方法最终将调用 NSObjectisEqual:< /代码>。基本上,它应该适用于 NSStringNSNumber 数组,也许还有 NSDate(还没有尝试过),但除此之外,YMMV。我想如果你尝试传递 UITableViewCell 数组或其他一些非常复杂的对象,它会完全崩溃,要么崩溃,要么返回完全垃圾。同样,如果您要执行诸如传递一个 NSDate 数组作为引用数组和一个 NSString 数组作为源数组之类的操作。此外,如果源数组包含引用数组中未涵盖的项目,它们将被丢弃。人们可以通过添加一些额外的代码来解决其中一些问题。

话虽如此,如果您尝试做一些简单的事情,它应该会很好地工作。在您的情况下,您可以在循环 setValue:forKey: 时构建引用数组。

NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];

for (//whatever you are looping through here)
{

    [yourDictionary setValue://whatever forKey:key];
    [referenceArray addObject:key];

}

然后,当您想按照项目出现的顺序循环遍历项目时,您只需

for (NSString *key in [self reorderArray:[myDict allKeys] toArray:referenceArray])

I wrote a quick method to take a source array (of objects that are all out of order) and a reference array (that has objects in a desired (and totally arbitrary) order), and returns an array where the items of the source array have been reorganized to match the reference array.

- (NSArray *) reorderArray:(NSArray *)sourceArray toArray:(NSArray *)referenceArray
{
    NSMutableArray *returnArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < [referenceArray count]; i++)
    {
        if ([sourceArray containsObject:[referenceArray objectAtIndex:i]])
        {
            [returnArray addObject:[arrReference objectAtIndex:i]];
        }
    }
    return [returnArray copy];
}

Note that this is very fragile. It uses NSArray's containsObject: method, which ultimately will call NSObject's isEqual:. Basically, it should work great for arrays of NSStrings, NSNumbers, and maybe NSDates (haven't tried that one yet), but outside of that, YMMV. I imagine if you tried to pass arrays of UITableViewCells or some other really complex object, it would totally sh*t itself, and either crash or return total garbage. Likewise if you were to do something like pass an array of NSDates as the reference array and an array of NSStrings as the source array. Also, if the source array contains items not covered in the reference array, they'll just get discarded. One could address some of these issues by adding a little extra code.

All that said, if you're trying to do something simple, it should work nicely. In your case, you could build up the reference array as you are looping through your setValue:forKey:.

NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];

for (//whatever you are looping through here)
{

    [yourDictionary setValue://whatever forKey:key];
    [referenceArray addObject:key];

}

Then, when you want to loop over your items in the order they came in, you just

for (NSString *key in [self reorderArray:[myDict allKeys] toArray:referenceArray])
青巷忧颜 2024-09-19 14:44:11

实际上你有一个按顺序引用的数组,那么为什么你必须添加一个数组。所以我想这种方法不好。请考虑我的意见。

Actually you have a reference array in order manner then why you have to add to one more array.So i guess this approach is not good.Please consider my opinion.

澜川若宁 2024-09-19 14:44:11

尽管 @GenralMike 的答案很简单,但可以通过省略不必要的代码来优化它,如下所示:

1)保留一个数组以按添加顺序保存对字典键的引用。

NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];

for (id object in someArray) {

     [yourDictionary setObject:object forKey:someKey];
     [referenceArray addObject:someKey]; // add key to reference array

}

2)现在“referenceArray”按顺序保存所有键,因此您可以按照最初添加到字典中的顺序从字典中检索对象。

for (NSString *key in referenceArray){
    //get object from dictionary in order
    id object = [yourDictionary objectForKey:key];
}

Although @GenralMike 's answer works a breeze, it could be optimized by leaving off the unnecessary code as follows:

1) Keep an array to hold reference to the dictionary keys in the order they are added.

NSMutableArray *referenceArray = [[NSMutableArray alloc] init];
NSMutableDictionary *yourDictionary = [[ NSMutableDictionary alloc] init];

for (id object in someArray) {

     [yourDictionary setObject:object forKey:someKey];
     [referenceArray addObject:someKey]; // add key to reference array

}

2) Now the "referenceArray" holds all of the keys in order, So you can retrieve objects from your dictionary in the same order as they were originally added to the dictionary.

for (NSString *key in referenceArray){
    //get object from dictionary in order
    id object = [yourDictionary objectForKey:key];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文