如何在不知道键的情况下迭代字典,同时获取键和对象?
我有一个 NSDictionary 并想要迭代对象。但同时,我需要知道字典的键。
我记得有一种特殊的、奇特的快速枚举形式,但忘记了确切的语法。
有人吗?
I have an NSDictionary and want to iterate over the objects. But at the same time, I need to know the key of the dictionary.
I remember there was a special, fancy form of fast enumeration, but have forgotten the exact syntax.
Anyone?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为你想要的是这样的:
取自 这里。
I think what you want is something like this:
Taken from here.
如果你喜欢积木,这也是一个不错的选择。
This is also a good choice if you like blocks.
keyEnumerator 返回一个对象,可让您迭代字典中的每个键。来自此处
keyEnumerator returns an object that lets you iterate through each of the keys in the dictionary. From here
据我所知,当您提出问题时,只有一种方法可以同时遍历键和值:
CFDictionaryApplyFunction
。使用这个 Core Foundation 函数涉及到循环体的丑陋的 C 函数指针和大量同样丑陋但免费的桥接转换。
@zekel 有现代的迭代方式字典。投票给他吧!
To my knowledge, at the time when you asked the question, there was only one way of traversing keys and values at the same time:
CFDictionaryApplyFunction
.Using this Core Foundation function involves an ugly C function pointer for the body of the loop and a lot of not-less-ugly, yet toll-free-bridged casting.
@zekel has the modern way of iterating a dictionary. Vote him up!