gpt4 book ai didi

objective-c - 通过索引访问 NSMutableDictionary 中的对象

转载 作者:IT老高 更新时间:2023-10-28 11:31:40 25 4
gpt4 key购买 nike

要按顺序显示 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 指出的错字。

关于objective-c - 通过索引访问 NSMutableDictionary 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1475716/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com