通过索引访问NSMutableDictionary中的对象 [英] Accessing objects in NSMutableDictionary by index

查看:214
本文介绍了通过索引访问NSMutableDictionary中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从NSMutableDictionary中依次显示键/值(在tableview中),我需要通过索引访问它们。如果通过索引访问可以给在那个索引的键,我可以得到的值。

To display key/values from an NSMutableDictionary sequentially (in a tableview), I need to access them by index. If access by index could give the key at that index, I could than get the value. Is there a way to do that or a different technique?

推荐答案

你可以使用一个包含对象的键的NSArray allKeys方法。然后你可以通过索引来查看。请注意,键在数组中显示的顺序是未知的。例如:

You can get an NSArray containing the keys of the object using the allKeys method. You can then look into that by index. Note that the order in which the keys appear in the array is unknown. Example:

NSMutableDictionary *dict;

/* Create the dictionary. */

NSArray *keys = [dict allKeys];
id aKey = [keys objectAtIndex:0];
id anObject = [dict objectForKey:aKey];

编辑:实际上,如果我明白你想做什么,快速枚举,例如:

Actually, if I understand what you're trying to do what you want is easily done using fast enumeration, for example:

NSMutableDictionary *dict;

/* Put stuff in dictionary. */

for (id key in dict) {
    id anObject = [dict objectForKey:key];
    /* Do something with anObject. */
}

编辑:Marco指出的固定拼写错误。

Fixed typo pointed out by Marco.

这篇关于通过索引访问NSMutableDictionary中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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