要按顺序显示NSMutableDictionary中的键/值(在tableview中),我需要通过索引访问它们。如果通过索引访问可以在该索引处给出关键字,那么我可以获得该值。有没有办法做到这一点或不同的技术?按索引访问NSMutableDictionary中的对象
回答
您可以使用包含对象的键的NSArray allKeys方法。然后你可以通过索引来查看。请注意,键在数组中显示的顺序未知。例如:
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. */
}
编辑:固定错字所指出的马可。
“allkeys”是“allKeys” – Mark 2011-04-09 22:25:19
在tableview中的'cellForRowAtIndexPath'中可能是:'NSArray * keys = [colors allKeys]; NSString * key = [keys objectAtIndex:indexPath.row]; cell = [tv dequeueReusableCellWithIdentifier:@“CellColour”forIndexPath:indexPath]; cell.textLabel.text = [colors objectForKey:key]; cell.backgroundColor = [self colorFromHexString:key];' – 2013-11-08 14:23:33
你可以得到所有字典的allKeys
方法键的数组;然后你可以通过索引访问数组。然而,字典本身并不具有固有的顺序,所以对字典进行更改前后得到的键的顺序可以完全不同
- 1. 按索引访问JavaScript对象元素
- 2. 访问/访问MVC中的IEnumerable对象中的特定索引
- 3. jQuery的访问对象通过索引
- 4. 按对象排序NSMutableDictionary键?
- 5. 在NSMutableDictionary中删除索引20后的所有对象?
- 6. 按索引访问属性
- 7. 对象的NSMutableDictionary
- 8. 按索引问题的Django Queryset访问
- 9. NSMutableArray访问NSMutableDictionary
- 10. 对象中的NSMutableDictionary和NSMutableArray对象
- 11. 访问ReferenceChange对象引用
- 12. C++对象引用/访问
- 13. 访问范围变量索引在Thymeleaf中的地图对象
- 14. 如何通过jQuery中的索引访问对象键?
- 15. 如何循环访问stdClass对象中的编号索引?
- 16. 如何访问C#中IEnumerable对象的索引?
- 17. 访问一个对象键和索引从for循环中的对象
- 18. 将对象添加到NSMutableDictionary的问题
- 19. 跨类访问NSMutableDictionary
- 20. 按索引排列的对象
- 21. 当从NSMutableArray更改为NSMutableDictionary时访问对象
- 22. 如何访问NSMutableDictionary的键?
- 23. NSMutableDictionary丢失对象
- 24. 按照编号/索引访问pictureBox
- 25. 按键和索引访问JSON
- 26. 红黑树访问按顺序索引
- 27. 按索引jsonparser访问字段
- 28. 按内容访问QVector元素索引
- 29. Python按值访问元组索引
- 30. MSBuild:比较ItemGroups或按索引访问
什么时候做:NSMutableDictionary * tmpDict = [[NSMutableDictionary alloc] init]; [tmpDict setObject:@“CN-GSM”forKey:@“Provider:”]; [tmpDict setObject:@“10010”forKey:@“Number:”]; [tmpDict setObject:@“Yes”forKey:@“Free:”]; NSLog(@“%@”,[tmpdict allKeys]); [tmpDict发布];为什么输出不按我添加到tmpDict的值排序? – ZYiOS 2010-12-02 05:46:28