要按顺序显示 NSMutableDictionary 中的键/值(在 TableView 中),我需要按索引访问它们。如果按索引访问可以提供该索引处的键,那么我可以得到该值。有没有办法做到这一点或不同的技术?
最佳答案
您可以使用 allKeys 方法获取包含对象键的 NSArray。然后,您可以按索引查看。请注意,键在数组中出现的顺序是未知的。示例:
NSMutableDictionary *dict;
/* Create the dictionary. */
NSArray *keys = [dict allKeys];
id aKey = [keys objectAtIndex:0];
id anObject = [dict objectForKey:aKey];
编辑:实际上,如果我了解您想要做什么,您可以使用快速枚举轻松完成,例如:
NSMutableDictionary *dict;
/* Put stuff in dictionary. */
for (id key in dict) {
id anObject = [dict objectForKey:key];
/* Do something with anObject. */
}
编辑:修正了 Marco 指出的错字。
https://stackoverflow.com/questions/1475716/